代码之家  ›  专栏  ›  技术社区  ›  Hunt

使用MVVMLight的ListView ItemTapEvent

  •  0
  • Hunt  · 技术社区  · 7 年前

    我正在尝试使用 Xamarin Forms MVVMLight . 我已经创建了包含菜单项的主详细信息页。 当用户点击菜单项时,我想导航页面。现在的问题是创建 ItemTapped ListView 使用 MVV灯 我试着在网上找答案,但找不到真正有效的答案。

    EventToCommand 使用 但是xaml说

    在类型交互中找不到可附加的属性行为

    <ListView 
                ItemsSource="{Binding MenuItemsList}"            
                CachingStrategy="RecycleElement"           
                Margin="0,0,0,10"
                RowHeight="110"
                SeparatorVisibility="Default"
                x:Name="MenuList"
    
                >
                <i:Interaction.Behaviors>
                    <i:BehaviorCollection>
                        <cmd:EventToCommand EventName="ItemSelected" Command="{Binding OnTapCommand}"                                   
                    </i:BehaviorCollection>
                </i:Interaction.Behaviors>
                  <DataTemplate>
                        <ViewCell>
                        </ViewCell>
                  </DataTemplate>
    </ListView>
    

    xmlns:i=“clr命名空间:System.Windows.interactive;assembly=System.Windows.interactive” xmlns:cmd=“clr命名空间:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras”

    MVV灯 我习惯于使用自定义行为,比如 behaviors:ItemTappedCommandBehavior.ItemTappedCommand="{Binding OnContactSelectCommand}"

    MVV灯 android和iOS平台

    1 回复  |  直到 7 年前
        1
  •  0
  •   Depechie    7 年前

    当显示菜单的列表视图中的项目时,您可以利用列表视图的内置功能来知道用户“选择”了什么。

    换句话说,只需将ListView的SelectedItem属性绑定到VM中的一个属性。

    所以XAML应该是:

    <ListView ItemsSource="{Binding MenuItemsList}"
              CachingStrategy="RecycleElement"
              Margin="0,0,0,10"
              RowHeight="110"
              SeparatorVisibility="Default"
              x:Name="MenuList"
              SelectedItem="{Binding SelectedMenuItem}">
        <DataTemplate>
            <ViewCell>
            </ViewCell>
        </DataTemplate>
    </ListView>