【问题标题】:MvvmCross vnext: CheckBox CheckedChange event to a command with monodroidMvvmCross vnext:CheckBox CheckedChange 事件到带有 monodroid 的命令
【发布时间】:2012-10-29 11:18:21
【问题描述】:

我正在尝试将来自 monodroid CheckBox 的 CheckedChange 绑定到一个命令,但出现错误。

我想在选中某一项时取消选择另一项。

我认为在 wp7 中使用 EventTrigger 是可以做到的,但是 MvvmCross for android 似乎不支持这个功能。

MvvmCross 是否仅限于按钮?

提前感谢您的帮助。

【问题讨论】:

    标签: windows-phone-7 xamarin.ios xamarin.android xamarin mvvmcross


    【解决方案1】:

    CheckedChangedEventHandler<CompoundButton.CheckedChangeEventArgs>,因此它不是 MvvmCross 自动知道的委托类型之一。


    但是,有一个自定义绑定用于此...

    https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxCompoundButtonCheckedTargetBinding.cs

    并且此自定义绑定应使用以下方式注册:

    registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(MvxCompoundButtonCheckedTargetBinding), typeof(CompoundButton), "Checked"));
    

    https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs


    所以如果你有一个 ViewModel 的属性 IsSpecial

    private bool _isSpecial;
    public bool IsSpecial
    {
       get { return _isSpecial; }
       set 
       {
          _isSpecial = value;
           RaisePropertyChanged(() => IsSpecial);
           // your custom code here
       }
    }
    

    那么这个绑定应该可以工作:

      'Checked':{'Path':'IsSpecial'} 
    

    这应该适用于任何 CompoundButton - CheckBox、Switch 或您自己的化合物...

    【讨论】:

    • 我想用 ICommand 代替这个属性。没关系,我改变了我的 ViewModel 来解决这个问题。
    • 如果您愿意,可以编写自定义绑定...或者您可以将点击绑定到某种切换命令?
    • 我使用 Click 事件而不是 CheckedChange 事件,它适用于我的命令。我更喜欢这种设计,而不是暂时使用属性绑定或编写新的自定义绑定。非常感谢。
    • @Stuart CompoundButton 不起作用。在注册时,我尝试将类型更改为 CheckBox - typeof(CheckBox),效果很好。
    • 如果您对答案有疑问,请编辑它。如果您在 mvx 中遇到错误,请将其记录在问题上 - 如果您包含一个 git repo,我可以在其中复制它,还有额外的奖励表情符号 :) 谢谢。斯图尔特
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多