【问题标题】:Nested MvxListView binding failure嵌套的 MvxListView 绑定失败
【发布时间】:2014-02-20 04:58:04
【问题描述】:

是否可以嵌套绑定 MvxListView(mvvmcross android v3.1 系列)?

我发现嵌套绑定失败:

MvxBind:Warning:  1.17 Unable to bind: source property source not found Property:Command on MenuSection

我们的 ViewModel 有点像

   class SomeViewModel : MvxViewModel{
       public List<MenuSection> Sections{get;set;}
   }

在哪里

class MenuSection{
   public string Title{get;set;}
   public MenuItem[] Items{get;set;}
}
class MenuItem{
   public string Name {get;set;}
   public ICommand Command{get;set;}
}

删除了大多数非 mvx 属性的 axml 的缩减版本如下所示:

layout/page_home.axml

<android.support.v4.widget.DrawerLayout>
  <FrameLayout android:id="@+id/content_frame"/>
  <Mvx.MvxListView
        local:MvxBind="ItemsSource Sections"
        local:MvxItemTemplate="@layout/item_menusection" />
</android.support.v4.widget.DrawerLayout>

layout/item_menusection.axml

<LinearLayout>
  <TextView local:MvxBind="Text Title"/>
  <Mvx.MvxListView
      local:MvxItemTemplate="@layout/item_menusection_item"
      local:MvxBind="ItemSource Items; ItemClick Command" />
</LinearLayout>

布局/item_menusection_item.axml

<TextView local:MvxBind="Text Name"/>

【问题讨论】:

    标签: mvvmcross


    【解决方案1】:

    这是因为您将内部 ListView 绑定到 MenuSection 对象。

    Command 属性在 MenuItem 对象中,而不是在 MenuSection 中。

    您需要将命令移动到 MenuSection。

    编辑:

    page_home.axml 绑定到 SomeViewModel =>

    ListView.Items 绑定到 SomeViewMode.Sections =>

    item_menusection.axml 中定义的每个项目都绑定到 MenuSection =>

    (在 item_menusection.axml 中)ListView.Items 绑定到 MenuSections.Items =>

    item_menusection_item.axml 中定义的每个项目都绑定到 MenuItem =>

    TextView.Text 绑定到 MenuItem.Name

    同样在 item_menusection.axml 中:ListView.ItemClick 绑定到 MenuSections.Command(它不存在)

    这是因为您如何定义 layout/item_menusection.axml

    <LinearLayout>
      <TextView local:MvxBind="Text Title"/>
      <Mvx.MvxListView
          local:MvxItemTemplate="@layout/item_menusection_item"
          local:MvxBind="ItemSource Items; ItemClick Command" />
    </LinearLayout>
    

    ListView 的 ItemSource 和 ItemClick 都绑定到同一个视图模型(MenuSections)。

    如果您想处理 MenuItem 视图模型中的项目点击,您可以通过以下几种方式执行此操作:

    您可以尝试使 item_menusection_item 中的布局可点击,并将点击事件绑定到 MenuItem.Command。

    或者,给 MenuSection 添加一个 Command 属性,然后依次调用所选 MenuItem 的 Command:

    public class MenuSection
    {
        public string Title{get;set;}
        public MenuItem[] Items{get;set;}    
        public ICommand Command { get { return new MvxCommand<MenuSection>((ms) => ms.Command.Execute(null)); } }
    }
    

    【讨论】:

    • 这没有意义; MenuSection 不应该有命令,菜单中的项目应该有。
    • 其次,page_home 绑定到 VM 中的 'Sections' 列表; item_menusection 因此绑定了一个 MenuSection。因此,如果对象层次结构正确绑定,则绑定“项目”应该可以工作
    • 不,ItemSource 和 ItemClick 都绑定到同一个视图模型 MenuSection。我编辑了我的回复以显示绑定流程。
    猜你喜欢
    • 2014-01-04
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多