MvvmCross从View看物业 [英] MvvmCross watch property from View

查看:189
本文介绍了MvvmCross从View看物业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Andr​​oid设备上查看触发的方法来创建一个吐司消息每当视图模型一定的属性更改。我发现所有的例子都在XML中绑定。这看起来非常简单的但我找不到任何的例子。

I want my android View to trigger a method to create a Toast message whenever a certain property changes in the ViewModel. All the examples I'm finding are binding within the XML. This seems so much simpler yet I can't find any examples.

推荐答案

您可以通过创建一个弱订阅的视图内的视图模型并显示敬酒时的财产变动情况如下做到这一点: -

You can do this by creating a weak subscription to the viewmodel inside the view and showing the toast when the property changes as follows:-

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
    {
        IMvxNotifyPropertyChanged viewModel = ViewModel as IMvxNotifyPropertyChanged;
        viewModel.WeakSubscribe(PropertyChanged);
        return base.OnCreateView(inflater, container, savedInstanceState);            
    }

    private void PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "the property")
        {
            //Display toast
        }
    }

我会被诱惑然而,而不是让你的视图模型来控制此行为(你打算写上面的code,每执行?)

I would be tempted however to instead allow your viewmodel to control this behaviour (are you going to write the above code for every implementation?)

只需添加 UserInteraction 插件通过的NuGet并执行以下操作:

Simply add the UserInteraction plugin via nuget and do the following:

private readonly IUserInteraction _userInteraction;

public FirstViewModel(IUserInteraction userInteraction)
{
    _userInteraction = userInteraction;
}

private string _hello = "Hello MvvmCross";
public string Hello
{ 
    get { return _hello; }
    set
    {
        _hello = value;
        RaisePropertyChanged(() => Hello);
        _userInteraction.Alert("Property Changed!");
    }
}

这不显示敬酒,它会显示一个消息框,但希望它会给你足够的去了。

This doesn't display a toast, it displays a message box, but hopefully it will give you enough to go off.

最后,你可以使用一个信使插件发送一个显示祝酒消息。

Finally you could use a messenger plugin to send a "Show toast" message.

这篇关于MvvmCross从View看物业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆