ViewModel.cs
public event EventHandler RecommendedScents;
private void _recommendedScents()
{
var handler = RecommendedScents;
if (handler != null)
handler(this, new System.EventArgs());
}
Activity.cs
我在视图中注册了
ViewModel.RecommendedScents -= SetRecommendedScents;
ViewModel.RecommendedScents += SetRecommendedScents;
在此处获取控制权:
private void SetRecommendedScents(object sender, EventArgs e)
{
}
答案 0 :(得分:3)
您不需要使用此类技巧将数据从ViewModel传递到View。只需将您的视图声明为MvxActivity<MyViewModel>
您的视图中将有一个属性ViewModel。
[Activity(ScreenOrientation = ScreenOrientation.Portrait)]
public class MyView : MvxActivity<MyViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var myValue = ViewModel.SomeProperty; // here you access your VM
}
}
如果您需要将数据从ViewModel发送到View,您需要使用像MvvmCross.Plugin.Messenger这样的事件聚合器系统
https://www.mvvmcross.com/documentation/plugins/messenger?scroll=1524