【问题标题】:Getting a switch Checked event to fire in the same way as a Click event with mvvmcross使用 mvvmcross 以与 Click 事件相同的方式触发 switch Checked 事件
【发布时间】:2015-03-02 11:06:00
【问题描述】:

我有一个用于 Android 的 mvvmcross 项目正在开发中,并且最近更改了部分 UI 以使用开关而不是按钮来从愿望清单中删除项目。默认情况下,开关设置为开。在按钮版本中,当按钮被按下时,项目会立即被移除。

按钮是使用 Click RemoveCommand 绑定的,而开关是使用设计器中的 Checked RemoveCommand 绑定的。

该按钮可以正常工作并删除该项目,但该开关会引发一个绑定警告,指出该目标属性是只读的。我想要做的是当开关改变时,以与单击按钮相同的方式从愿望列表中删除该项目。

有没有办法让选中的事件以与 Click 事件相同的方式运行?

【问题讨论】:

    标签: c# xamarin xamarin.android mvvmcross


    【解决方案1】:

    最简单的答案:

    • 我认为 Checked 是一个布尔属性的绑定 - 所以你可以将视图上的 Checked 绑定到 ViewModel 上的布尔值(然后在 set 处理程序中处理一些逻辑,如果你需要)。

    • 或者,如果你想要一个简单的无参数Command 绑定,那么你可以只使用Click


    如果您想做一个Command<bool> 绑定,那么您可以使用https://github.com/MvvmCross/MvvmCross/blob/c306ba37afd9024f68b7a4f1fedcaf4cf7d01b8d/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxCompoundButtonCheckedTargetBinding.cshttps://github.com/MvvmCross/MvvmCross/blob/c306ba37afd9024f68b7a4f1fedcaf4cf7d01b8d/Cirrious/Cirrious.MvvmCross.Binding.Droid/Target/MvxViewClickBinding.cs 中的代码作为模板快速创建一个 - 类似于:

        using System.Reflection;
        using Android.Widget;
        using Cirrious.CrossCore.Platform;
        using Cirrious.MvvmCross.Binding.Bindings.Target;
    
        namespace MyApp.Ext.Target
        {
            public class MyCompoundButtonCheckedTargetBinding
                : MvxAndroidTargetBinding
            {
                private ICommand _command;
                private IDisposable _canExecuteSubscription;
                private readonly EventHandler<EventArgs> _canExecuteEventHandler;
    
                protected CompoundButton View
                {
                    get { return (CompoundButton)Target; }
                }
    
                public MyCompoundButtonCheckedTargetBinding(View view)
                    : base(target, targetPropertyInfo)
                {
                    _canExecuteEventHandler = new EventHandler<EventArgs>(OnCanExecuteChanged);
                    SubscribeToEvents();
                }
    
                public override MvxBindingMode DefaultMode
                {
                    get { return MvxBindingMode.OneWay; }
                }
    
                public override Type TargetType
                {
                    get { return typeof(ICommand); }
                }
    
                public override void SubscribeToEvents()
                {
                    var compoundButton = View;
                    if (compoundButton == null)
                    {
                        MvxBindingTrace.Trace(MvxTraceLevel.Error,
                                              "Error - compoundButton is null in MvxCompoundButtonCheckedTargetBinding");
                        return;
                    }
    
                    _subscribed = true;
                    compoundButton.CheckedChange += CompoundButtonOnCheckedChange;
                }
    
                private void RefreshEnabledState()
                {
                    var view = View;
                    if (view == null)
                        return;
    
                    var value = view.Checked;
    
                    var shouldBeEnabled = false;
                    if (_command != null)
                    {
                        shouldBeEnabled = _command.CanExecute(value);
                    }
                    view.Enabled = shouldBeEnabled;
                }
    
                private void OnCanExecuteChanged(object sender, EventArgs e)
                {
                    RefreshEnabledState();
                }
    
                protected override void SetValueImpl(object target, object value)
                {
                    if (_canExecuteSubscription != null)
                    {
                        _canExecuteSubscription.Dispose();
                        _canExecuteSubscription = null;
                    }
                    _command = value as ICommand;
                    if (_command != null)
                    {
                        _canExecuteSubscription = _command.WeakSubscribe(_canExecuteEventHandler);
                    }
                    RefreshEnabledState();
                }
    
                private void CompoundButtonOnCheckedChange(object sender, CompoundButton.CheckedChangeEventArgs args)
                {
                    if (_command == null)
                        return;
    
                    var value = args.IsChecked; /* TODO - check this name... */
    
                    if (!_command.CanExecute(value))
                        return;
    
                    _command.Execute(value);
                }
    
                protected override void Dispose(bool isDisposing)
                {
                    if (isDisposing)
                    {
                        var view = View;
                        if (view != null)
                        {
                            view.CheckedChange -= CompoundButtonOnCheckedChange;
                        }
                        if (_canExecuteSubscription != null)
                        {
                            _canExecuteSubscription.Dispose();
                            _canExecuteSubscription = null;
                        }                
                    }
                    base.Dispose(isDisposing);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 2023-03-23
      • 1970-01-01
      • 2012-04-09
      • 2010-10-19
      • 1970-01-01
      • 2015-10-30
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多