如何在 xamarin android 中将数据从 ViewModel 传递到 Activity
How to pass the data from ViewModel to Activity in xamarin android
我正在尝试做的事情:将数据从 ViewModel 传递到 Activity
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)
{
}
您不需要使用这种技巧将数据从 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
我正在尝试做的事情:将数据从 ViewModel 传递到 Activity
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)
{
}
您不需要使用这种技巧将数据从 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