gpt4 book ai didi

android - MvxBind CheckedChange SwitchCompat

转载 作者:行者123 更新时间:2023-11-29 19:26:36 29 4
gpt4 key购买 nike

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

我在查看 View 时遇到的错误如下:

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的引用。我这样做了,但错误仍然存​​在。

LinkerPleaseInclude

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;
}
}

我的 View 布局:

<?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>

最佳答案

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

这看起来像:

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 绑定(bind)到您的命令。

关于android - MvxBind CheckedChange SwitchCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41259474/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com