【问题标题】:Handling ViewModel Property Changed in Xamarin Forms Content Page处理 Xamarin 表单内容页面中更改的 ViewModel 属性
【发布时间】:2018-03-28 06:19:34
【问题描述】:

我想在我的 Xamarin Forms ContentPage 代码后面的 ViewModel 中设置一个属性(使用 INotifyPropertyChanged)。

假设我可以访问我的 ViewModel,我“可以”这样做 -

    protected override void OnAppearing()
    {
        base.OnAppearing();

        // My base class enabled ViewModel access
        ViewModel.PropertyChanged += (sender, e) =>
        {
            Debug.WriteLine(e.PropertyName + " Changed on ViewModel");

            if (e.PropertyName == "Bob")
            {
                DoSomething();
            }
        };

我是否必须考虑从 ViewModel dispose 上的 PropertyChanged 事件中取消注册?

还有其他更优雅的方法吗?

例如,我看到提到的使用 Rx Extensions 实现它的方法,我使用了 MvxPropertyChangedListener 和 Mvvmcross 来帮助解决这种事情。

作为对@Jesus Angulo 的回应,我现在这样做的原因是当我的项目列表发生更改时,以编程方式在滚动视图中填充 StackLayout,这样我就不必设置任何自定义绑定代码。

感谢您的帮助。

【问题讨论】:

  • 你想用这个实现什么?因为这取决于。

标签: xamarin xamarin.forms inotifypropertychanged


【解决方案1】:

如果您真的必须这样做,我建议您使用消息中心在您在后面的页面中订阅的视图模型中发布消息。

Here is a how to on Messaging Center

但确实必须有一种方法可以在视图模型中完成所有这些操作。

你不能将 StackLayout 更改为 ListView 并在属性更改时填充事物列表吗?

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多