MvvmCross vnext: monodroid binding Dictionary Key similar to wp7

瘦欲@ 提交于 2019-12-24 15:50:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!