【问题标题】:MvvmCross vnext: monodroid binding Dictionary Key similar to wp7MvvmCross vnext:类似于wp7的monodroid绑定Dictionary Key
【发布时间】:2012-10-15 16:07:36
【问题描述】:

当我在 Tutorial.Core 中更改 MainMenuViewModel 以使用这样的字典时:

`公共词典项目{get;放; } 公共 ICommand ShowItemCommand { 得到 { 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)}
                };
}`

示例在 wp7 中按预期工作,但使用 monodroid 我得到一个错误::"MvxBind:Error: 2,71 Problem seen during binding execution for from Items to ItemsSource - 问题 ArgumentException: failed to convert parameters" 因为我认为 KeyValuePair Key 属性导致问题的原因在于:

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

提前感谢您的帮助。

【问题讨论】:

    标签: c# windows-phone-7 xamarin.ios xamarin.android mvvmcross


    【解决方案1】:

    问题在于 mvxbindablelistview 需要一个支持 IList 接口的对象 - 因此它目前无法绑定到字典。

    这是 'ArgumentException: failed to convert parameters' 告诉我们的。


    如果你想使用字典,那么你可以应用一个将字典映射到 List() 的转换器


    如果您认为这是 mvx 中缺少的功能 - 如果您认为列表应该绑定到任何 ienumerable(或者可能绑定到任何 icollection),那么请在 github 上记录这是一个问题。


    更新 - 已在 https://github.com/slodge/MvvmCross/issues/38 上进行了更新 - 现在行为已更改。

    【讨论】:

    • 好的,我现在明白这个错误了。所以我使用 List> 而不是 Dictionary. 绕过了这个问题
    • 是的 - 一直在看这个。不支持 icollection 和 ienumerable 的动机似乎很好 - 基本上 ilist 添加了 operator[] 索引访问,这允许 listview 比使用 ienumerable 更有效地运行。我们可以添加 ienumerable 支持,它可以更好地遵守 wp7,但会导致列表视图效率低下。
    猜你喜欢
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多