【问题标题】:MvxBind CheckedChange SwitchCompatMvxBind CheckedChange SwitchCompat
【发布时间】:2017-05-06 16:22:05
【问题描述】:

我在将Android.Support.V7.Widget.SwitchCompat 绑定到我的视图模型时遇到了麻烦(Mvvmcross 是我正在使用的框架) 我所做的与另一个对象上的单击绑定完全相同,效果很好。

查看视图时出现的错误如下:

12-21 10:32:06.459 I/MvxBind (22969):  42,82 Failed to create target binding for binding CheckedChange for OnCheckedChanged
[0:] MvxBind:Warning: 42,82 Failed to create target binding for binding CheckedChange for OnCheckedChanged

我拥有的开关数量是多次。

他们说它可能不得不对链接器做一些事情,因为反射魔法不包括东西。

这样他们说您必须创建一个文件“LinkerPleaseInclude”以保留对您的 switchcompat 的引用。我这样做了,但错误仍然存​​在。

链接器请包含

class LinkerPleaseInclude
{
    public void Include(TextView text)
    {
        text.AfterTextChanged += (sender, args) => text.Text = "" + text.Text;
        text.Hint = "" + text.Hint;
    }

    public void Include(CompoundButton cb)
    {
        cb.CheckedChange += (sender, args) => cb.Checked = !cb.Checked;
        cb.Hint = "" + cb.Hint;
    }
    public void Include(SwitchCompat cb)
    {
        cb.CheckedChange += (sender, args) => cb.Checked = !cb.Checked;
        cb.Hint = "" + cb.Hint;
    }
    public void Include(ICommand command)
    {
        command.CanExecuteChanged += (s, e) => { if (command.CanExecute(null)) command.Execute(null); };
    }
    public void Include(CheckBox checkBox)
    {
        checkBox.CheckedChange += (sender, args) => checkBox.Checked = !checkBox.Checked;
    }
}

我的视图布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/md_list_single_line_item_height"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/md_list_item_horizontal_edges_padding"
    android:paddingRight="@dimen/md_list_item_horizontal_edges_padding">
  <android.support.v7.widget.SwitchCompat
    android:id="@+id/mySwitch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    local:MvxBind="Checked IsActive; Click OnSwitchClick; CheckedChange OnCheckedChanged" />
  <TextView
    android:id="@+id/Name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:textColor="@color/md_text_dark_primary_87"
    android:textSize="@dimen/md_list_item_primary_text"
    local:MvxBind="Text Name"/>
  <TextView
    android:id="@+id/Kind"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:textColor="@color/md_text_dark_secondary_54"
    android:textSize="@dimen/md_list_item_secondary_text"
    android:layout_below="@+id/Name"
    local:MvxBind="Text Kind"/>
</RelativeLayout>

此布局是另一个布局的子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="@dimen/md_list_two_line_item_height"
  android:paddingTop="?android:attr/actionBarSize"
  android:fitsSystemWindows="true">
  <MvxClickableLinearLayout
      android:id="@+id/animalSelectionsList"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:divider="@drawable/divider_horizontal"
      android:showDividers="middle"
      local:MvxBind="ItemsSource SelectionsList"
      local:MvxItemTemplate="@layout/listitem_animal_selections" />
</LinearLayout>

【问题讨论】:

  • 我不确定,但可能是因为 android.support.v7.widget.SwitchCompat 与复选框无关?它只是不支持CheckedChange? developer.android.com/reference/android/support/v7/widget/…
  • SwitchCompat 是 CompoundButton 的派生类型。就像复选框一样。 developer.android.com/reference/android/widget/… CompoundButton 有一个事件 CheckedChange
  • 只能要求查看您的模型。对于链接器的东西,尝试在您的项目设置中设置不同的 android 选项,以便您的链接器为 sdk 和用户程序集链接或不链接(不确定 atm 的默认设置是什么)。只是为了测试是否是链接器故障。
  • @Cyriac 我已将我的视图模型上传到 pastebin,因为它是一个非常大的模型,否则我会收到“您的问题包含的代码多于细节”错误 pastebin.com/JxGdcF0D 注意这是内部视图模型(在列表中)如果需要,我也可以上传父视图模型。设置设置为无
  • 我在我的项目中重新创建了它,但它对我也不起作用。所以你要么等我明天再看一遍,要么在他们的 github 上问,在那里你很可能会得到更快的答案。

标签: android xamarin data-binding bind mvvmcross


【解决方案1】:

CheckedChange 是一个事件,因此它不是公共财产。您不能直接在 MvvmCross 中绑定事件,除非您创建自己的目标绑定来处理此事件并公开命令。

这可能看起来像:

public class MvxCompoundButtonCheckedChangeBinding : MvxAndroidTargetBinding
{
    private ICommand _command;
    private IDisposable _checkedChangeSubscription;
    private IDisposable _canExecuteSubscription;
    private readonly EventHandler<EventArgs> _canExecuteEventHandler;

    protected CompoundButton View => (CompoundButton)Target;

    public MvxCompoundButtonCheckedChangeBinding(CompoundButton view)
        : base(view)
    {
        _canExecuteEventHandler = OnCanExecuteChanged;
        _checkedChangeSubscription = view.WeakSubscribe<CompoundButton, CompoundButton.CheckedChangeEventArgs>(nameof(view.CheckedChange), ViewOnCheckedChangeClick);
    }

    private void ViewOnCheckedChangeClick(object sender, CompoundButton.CheckedChangeEventArgs args)
    {
        if (_command == null)
            return;

        if (!_command.CanExecute(null))
            return;

        _command.Execute(view.Checked);
    }

    protected override void SetValueImpl(object target, object value)
    {
        _canExecuteSubscription?.Dispose();
        _canExecuteSubscription = null;

        _command = value as ICommand;
        if (_command != null)
        {
            _canExecuteSubscription = _command.WeakSubscribe(_canExecuteEventHandler);
        }
        RefreshEnabledState();
    }

    private void RefreshEnabledState()
    {
        var view = View;
        if (view == null)
            return;

        var shouldBeEnabled = false;
        if (_command != null)
        {
            shouldBeEnabled = _command.CanExecute(null);
        }
        view.Enabled = shouldBeEnabled;
    }

    private void OnCanExecuteChanged(object sender, EventArgs e)
    {
        RefreshEnabledState();
    }

    public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;

    public override Type TargetType => typeof(ICommand);

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            _checkedChangeSubscription?.Dispose();
            _checkedChangeSubscription = null;

            _canExecuteSubscription?.Dispose();
            _canExecuteSubscription = null;
        }
        base.Dispose(isDisposing);
    }
}

然后您需要在 FillTargetFactories 覆盖的 Setup.cs 中注册它:

registry.RegisterCustomBindingFactory<View>("MyCheckedChange", 
    view => new MvxCompoundButtonCheckedChangeBinding(view));

然后您可以将 MyCheckedChange 绑定到您的命令。

【讨论】:

【解决方案2】:

我只用 SwitchCompat 替换了 CompoundButton,所以它现在可以工作(并重命名类):

public class MvxButtonCheckedChangeBinding : MvxAndroidTargetBinding
{
    private ICommand _command;
    private IDisposable _checkedChangeSubscription;
    private IDisposable _canExecuteSubscription;
    private readonly EventHandler<EventArgs> _canExecuteEventHandler;

    public static string Name = "MyCheckedChange";

    protected CompoundButton View => (SwitchCompat)Target;

    public MvxButtonCheckedChangeBinding(SwitchCompat view)
        : base(view)
    {
        _canExecuteEventHandler = OnCanExecuteChanged;
        _checkedChangeSubscription = view.WeakSubscribe<SwitchCompat, SwitchCompat.CheckedChangeEventArgs>(nameof(view.CheckedChange), ViewOnCheckedChangeClick);
    }

    private void ViewOnCheckedChangeClick(object sender, SwitchCompat.CheckedChangeEventArgs args)
    {
        var view = (SwitchCompat)sender;

        if (view == null || _command == null)
            return;

        if (!_command.CanExecute(null))
            return;

        _command.Execute(view.Checked);
    }

    protected override void SetValueImpl(object target, object value)
    {
        _canExecuteSubscription?.Dispose();
        _canExecuteSubscription = null;

        _command = value as ICommand;
        if (_command != null)
        {
            _canExecuteSubscription = _command.WeakSubscribe(_canExecuteEventHandler);
        }
        RefreshEnabledState();
    }

    private void RefreshEnabledState()
    {
        var view = View;
        if (view == null)
            return;

        var shouldBeEnabled = false;
        if (_command != null)
        {
            shouldBeEnabled = _command.CanExecute(null);
        }
        view.Enabled = shouldBeEnabled;
    }

    private void OnCanExecuteChanged(object sender, EventArgs e)
    {
        RefreshEnabledState();
    }

    public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;

    public override Type TargetType => typeof(ICommand);

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            _checkedChangeSubscription?.Dispose();
            _checkedChangeSubscription = null;

            _canExecuteSubscription?.Dispose();
            _canExecuteSubscription = null;
        }
        base.Dispose(isDisposing);
    }
}

当然不要忘记像 Cheesebaron 上面写的那样在 setup 类中注册它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多