【发布时间】:2015-02-16 13:20:50
【问题描述】:
我做了一个自定义视图(MyCustomView),可以在android layout xml中使用。当您单击它时,它会打开一个包含列表的对话框 (MyDialog)。
MainPageLayout -> MyCustomView -> click -> MyDialog
MyDialog 继承自 MvxDialogFragment:
public class MyDialog : MvxDialogFragment
{
public event EventHandler ItemSourceChanged;
public ObservableCollection<MyItem> ItemsSource
{
get
{
return itemsSource;
}
set
{
itemsSource = value;
if (ItemSourceChanged != null)
{
ItemSourceChanged.Invoke(this, new EventArgs());
}
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.my_layout, container, false);
var listview = view.FindViewById<MvxListView>(Resource.Id.my_listview);
listview.ItemsSource = ItemsSource;
return view;
}
}
视图模型:
public class MyViewModel : MvxViewModel
{
public ObservableCollection<MyItem> MyCollection
{
get { return myCollection; }
private set { SetProperty(ref myCollection, value); }
}
}
到目前为止,一切都很好 - 这可以按预期工作。在视图中创建绑定集,并显示 VM 的“MyCollection”列表。
但是,我非常希望将 MyDialog 与其他不同的集合一起使用,而不仅仅是 ObservableCollection<MyItem>。为此,我将 MyDialog 中的 ItemsSource 更改为 ObservableCollection<object>,但这会导致其设置器不再被调用(由绑定集),因此列表视图的 itemSource 设置为 null。
如何使 MyDialog 更通用?
【问题讨论】:
-
怀疑您可能在某处打错了字?不确定
To achieve this, i change the ItemsSource in MyDialog to be a ObservableCollection是否有意义? -
啊,是的,泛型类型被切断了。我已经改变了格式。我将集合从包含“MyItem”的元素更改为“object”的元素。希望它现在更有意义。
标签: c# android mvvm xamarin mvvmcross