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

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

问题描述

当我尝试使用 MvvmCross 框架将我的 Android 按钮的启用"属性绑定到我的 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 属性具有 false 值的原因.但是对于我的 ViewModel 代码,在填充 ProjectDetailDTO 时应该更新 HasPicture 属性.我在 Android 视图中做错了什么吗?

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天全站免登陆