将Android按钮的Enabled属性与MvvmCross绑定 [英] Binding the Enabled property of a Android button with the MvvmCross

查看:62
本文介绍了将Android按钮的Enabled属性与MvvmCross绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用MvvmCross框架将Android Button的"Enabled"属性绑定到ViewModel的布尔值时遇到问题,我真的不知道它的起源.

I have a problem when I try to bind the "Enabled" property of my Android Button to a Boolean of my ViewModel using the MvvmCross framework and I really don't know the origin of it.

所以我有一个ViewModel,其中包含以下两个属性:

So I have a ViewModel which contains the two following properties :

private ProjectDetailDTO _projectDetail;
    public ProjectDetailDTO ProjectDetail
    {
        get { return this._projectDetail; }
        set 
        { 
            _projectDetail = value; 
            RaisePropertyChanged(() => ProjectDetail);
            RaisePropertyChanged(() => HasPicture);
        }
    }

    private bool _hasPicture;
    public bool HasPicture
    {
        get { return ((this.ProjectDetail != null) && !String.IsNullOrEmpty(this.ProjectDetail.Pictures)); }
        set { _hasPicture = value;
            RaisePropertyChanged(() => HasPicture); 
        }
    }

您将理解,我的按钮绑定到HasPicture属性.因此,我的.axml文件中的按钮具有以下代码:

As you would understand, my button is bind to the HasPicture property. So I have the following code for my button in my .axml file :

<Button
    local:MvxLang="Text LblSeePicturesValue"
    local:MvxBind="Enabled HasPicture,Click ShowProjectPicturesCommand"
    android:id="@+id/buttonPictures"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

我不认为这是ViewModel的问题,因为我的WP应用程序可以很好地使用此代码.实际上,我的ProjectDetailDTO是通过调用Web服务填充的,因此是通过异步方法填充的.我认为这就是为什么实现绑定时HasPicture属性具有错误值的原因.但是,使用我的ViewModel代码,在填充ProjectDetailDTO时应更新HasPicture属性.我在Android View中做错了什么吗?

I don't think it's a ViewModel problem because my WP application works well with this code. In fact, my ProjectDetailDTO is filled by calling a web service, so by an asynchronous method. I think it's why when the binding is realized the HasPicture property has the false value. But with my ViewModel code, the HasPicture property should be updated when the ProjectDetailDTO is filled. Is there anything I did wrong in my Android View?

感谢您的帮助!

推荐答案

我认为您在这里看到的是ICommand.CanExecuteEnabled属性之间的某种相互作用.在 https://github.com/MvvmCross/MvvmCross/issues/729

I think what you are seeing here is some interaction between ICommand.CanExecute and the Enabled property. There's a discussion about this on https://github.com/MvvmCross/MvvmCross/issues/729

要解决此问题,请尝试将绑定切换到:

To work around this, try switching the binding to:

local:MvxBind="Click ShowProjectPicturesCommand;Enabled HasPicture"

(还要注意,绑定中的分隔符是;-而不是,)

(Also note that the separator in bindings is a ; - not a ,)

这篇关于将Android按钮的Enabled属性与MvvmCross绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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