我创建了自己的自定义视图。此多列表选择器从列表中选择多行。
当他尝试绑定数据时,会出现此错误。
“ SetValue失败,发生异常-ArgumentException:类型为'System.Collections.Generic.List 1[System.Object]' cannot be converted to type 'System.Collections.Generic.List
1 [isvk.Core.Models.NtdChecked]'的对象”“
我的控制权
<isvk.Controls.MultipleListPicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/report_field_margin_top_bottom"
local:lp_label="@string/ReportMTRDataNtd"
local:lp_icon="@drawable/icon_list"
local:lp_targetProperty="Title"
local:MvxBind="Items Ntds; CheckedItems NtdCheckedList, Converter=ListObject; NewItem AddNewNtdItemCommand" />
我的转换器:
public class ListObjectConverter : MvxValueConverter
{
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
我创建了自己的Converter,但无法连接。在调试模式下,程序不会进入转换器。在下面的代码中可以更改的位置或内容。
我的MultipleListPicker:
public class MultipleListPicker : ListPicker
{
private readonly IList<object> _checkedItemsCancelable = new List<object>();
private IEnumerable<object> _checkedItems;
public MultipleListPicker(Context context, IAttributeSet attrs)
: base(context, attrs)
{
_listView.ChoiceMode = ChoiceMode.Multiple;
_dialog = new AlertDialog.Builder(Context).SetView(_pickerListView).SetCancelable(true)
.SetPositiveButton(Resource.String.ReadyText, OnReadyClicked)
.SetNegativeButton(Resource.String.CancelText, (sender, args) => _dialog.Dismiss()).Create();
}
public IEnumerable CheckedItems
{
get { return _checkedItems; }
private set
{
_checkedItems = new List<object>((IEnumerable<object>) value);
SelectionChanged?.Invoke(this, new MultipleSelectionEventArgs(_checkedItems));
}
}
protected override void OnClick(object sender, EventArgs args)
{
_listView.ClearChoices();
_checkedItemsCancelable.Clear();
for (var i = 0; i < _listView.ChildCount; i++)
{
SetDeselectedBackground(i);
}
if (CheckedItems != null)
{
foreach (var obj in CheckedItems)
{
SelectItem(obj);
}
}
base.OnClick(sender, args);
}
protected override void ListViewOnItemSelected(object obj)
{
if (!IsItemSelected(obj))
{
SelectItem(obj);
}
else
{
DeselectItem(obj);
}
}
private void SetSelectedBackground(int index)
{
try
{
var v = _listView.GetChildAt(index);
v.SetBackgroundResource(Resource.Color.colorPrimary);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
private void SelectItem(object obj)
{
var index = _listView.Adapter.GetPosition(obj);
_listView.SetItemChecked(index, true);
_checkedItemsCancelable.Add(obj);
SetSelectedBackground(index);
}
private void SetDeselectedBackground(int index)
{
var v = _listView.GetChildAt(index);
v.SetBackgroundResource(Resource.Color.white);
}
private void DeselectItem(object obj)
{
var index = _listView.Adapter.GetPosition(obj);
_listView.SetItemChecked(index, false);
_checkedItemsCancelable.Remove(obj);
SetDeselectedBackground(index);
}
private bool IsItemSelected(object obj)
{
return _checkedItemsCancelable.Contains(obj);
}
private void OnReadyClicked(object sender, DialogClickEventArgs eventArgs)
{
var hasSelection = false;
var builder = new StringBuilder(string.Empty);
foreach (var checkedItem in _checkedItemsCancelable)
{
hasSelection = true;
builder.Append(checkedItem);
builder.Append("; ");
}
if (hasSelection)
{
_contentTextView.Text = builder.ToString();
}
else
{
_contentTextView.SetText(Resource.String.NotSelectedText);
}
CheckedItems = _checkedItemsCancelable;
}
public event EventHandler<MultipleSelectionEventArgs> SelectionChanged;
}
public class MultipleSelectionEventArgs : EventArgs
{
public MultipleSelectionEventArgs(IEnumerable checkedItems)
{
CheckedItems = checkedItems;
}
public IEnumerable CheckedItems { get; }
}
我的项目结构: enter image description here