【发布时间】:2016-12-08 03:26:23
【问题描述】:
我在 VS2015 中使用 Xamarin.Forms Portable 解决方案。我使用 MVVM Light 将 PCL 中的内容页面绑定到 ViewModel。如果我现在更改 ViewModel 中的一个属性,它将更新 Android、IOS 和 UWP 上的 UI。但是,我被困在一个地方,我不确定如何实现更新 ViewModel 属性。
在我的机器人项目 MainActivity 中,我使用从 Activity 继承的 OnNewIntent 注册了一个新意图。只要调用了这个OnNewIntent,我就想更改ViewModel 中的属性,例如public string example。我不能简单地使用
ViewModel vm = new ViewModel();
vm.example = "Hi from Android";
当我创建 ViewModel 的新实例时,我的 DataBinding 正在引用另一个实例。那么这样做的正确方法是什么?我想过在我的 ViewModel 属性的 getter 中使用 DependencyInjection,比如
public string example
{get {%using dependency injection here to get the string from Android }}
但是我还必须通知 ViewModel 在 MainActivity 中调用 OnNewIntent 时必须更新绑定。
所以我会非常感谢任何建议如何以一种好的方式组织这个。
【问题讨论】: