问题
When I change MainMenuViewModel in Tutorial.Core to use a Dictionary like this:
`public Dictionary Items { get; set; } public ICommand ShowItemCommand { get { return new MvxRelayCommand>((type) => DoShowItem(type.Value)); } }
public void DoShowItem(Type itemType)
{
this.RequestNavigate(itemType);
}
public MainMenuViewModel()
{
Items = new Dictionary<string, Type>()
{
{"SimpleTextProperty", typeof(Lessons.SimpleTextPropertyViewModel)},
{"PullToRefresh", typeof(Lessons.PullToRefreshViewModel)},
{"Tip", typeof(Lessons.TipViewModel)},
{"Composite",typeof(Lessons.CompositeViewModel)},
{"Location",typeof(Lessons.LocationViewModel)}
};
}`
The sample is working as expected in wp7, but with monodroid I get an error::"MvxBind:Error: 2,71 Problem seen during binding execution for from Items to ItemsSource - problem ArgumentException: failed to convert parameters" because I think KeyValuePair Key property causes the problem in:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="View Model:"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
local:MvxBind="{'Text':{'Path':'Key'}}"
/>
</LinearLayout>
Thanks in advance for your help.
回答1:
The problem is that the mvxbindablelistview expects an object that supports the IList interface - so it can't currently bind to a Dictionary.
This is what 'ArgumentException: failed to convert parameters' tells us.
If you want to use a dictionary, then you could apply a converter that maps the dictionary to a List()
If you think this is a missing feature in mvx - if you feel lists should bind to any ienumerable (or maybe to any icollection), then please log this is an issue on github.
Update - this has been pursued on https://github.com/slodge/MvvmCross/issues/38 - and the behavior is now changed.
来源:https://stackoverflow.com/questions/12899593/mvvmcross-vnext-monodroid-binding-dictionary-key-similar-to-wp7