I think it is quite possible to access everything from view to viewmodel. If you are unable to do that in certain situation then, you can use event
in your VM and raise that event when you need and you can bind a method that you want to call. Please check my sample code.
public class View
{
ViewModel myVM = null;
public View()
{
myVM=new ViewModel();
myVM.CallMyMethodEvent += myViewMethod;
}
void myViewMethod(bool param)
{
//do you thing
}
}
public class ViewModel
{
public Action<bool> CallMyMethodEvent;
private void RaiseEventToCallMethodInView()
{
if (CallMyMethodEvent != null)
{
CallMyMethodEvent.Invoke(true);
}
}
}
New Update Code as you want to call MyMethod()
with 2 parameters. So, the code will look like =>
public class View
{
ViewModel myVM = null;
public View()
{
myVM.CallMyMethodWithEventArgEvent += MyMethod;
}
private void MyMethod(object sender, EventArgs e)
{
if(sender is StackLayout)
{
var yourStackLayoutobject = (StackLayout)sender;
}
}
}
public class ViewModel
{
public Action<object, EventArgs> CallMyMethodWithEventArgEvent;
private void RaiseEventToCallMethodWithEventArgInView()
{
if (CallMyMethodWithEventArgEvent != null)
{
CallMyMethodWithEventArgEvent.Invoke(new StackLayout(),null);
}
}
}
public class StackLayout
{
}
NOTE: Please check the code and let me know. I have used a dummy class StackLayout
you can use your proper class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…