Mvvm 使用Prism Xamarin表单创建动态选项卡页

Mvvm 使用Prism Xamarin表单创建动态选项卡页,mvvm,dynamic,xamarin.forms,prism,tabbedpage,Mvvm,Dynamic,Xamarin.forms,Prism,Tabbedpage,我正在使用Prism和item source,在每个选项卡式页面中都有一个Listview,当点击Listview项时,我无法将“ItemTapped”事件作为事件到命令行为来触发viewmodel中的命令,但当点击列表视图项时,在调试模式下没有触发器,请帮助我理解为什么会发生这种情况,以及是否有其他方法可以使用该命令进行此操作。此外,当我检查时,事件在PrismTabbedPage1.xaml.cs中触发,但在PrismTabbedPage1ViewModel.cs中未触发 棱镜沙马林型 Pr

我正在使用Prism和item source,在每个选项卡式页面中都有一个Listview,当点击Listview项时,我无法将“ItemTapped”事件作为事件到命令行为来触发viewmodel中的命令,但当点击列表视图项时,在调试模式下没有触发器,请帮助我理解为什么会发生这种情况,以及是否有其他方法可以使用该命令进行此操作。此外,当我检查时,事件在PrismTabbedPage1.xaml.cs中触发,但在PrismTabbedPage1ViewModel.cs中未触发

棱镜沙马林型

PrismTabbedPage1.Xaml

<?xml version="1.0" encoding="utf-8" ?>
 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" xmlns:behaviors="clr-namespace:Prism.Behaviors;assembly=Prism.Forms" prism:ViewModelLocator.AutowireViewModel="True" x:Class="testapp.Views.PrismTabbedPage1" ItemsSource="{Binding countries}"> <TabbedPage.ItemTemplate> 
<DataTemplate> 
<ContentPage Title="{Binding CountryName}">
 <ListView ItemsSource="{Binding Cities}"> 
<ListView.ItemTemplate>
 <DataTemplate>
 <ViewCell> 
<Label Text="{Binding CityName}" TextColor="Black"/> 
</ViewCell>
 </DataTemplate>
 </ListView.ItemTemplate> 
<ListView.Behaviors> 
<behaviors:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ListItemTapped}" EventArgsParameterPath="Item"/> 
</ListView.Behaviors> </ListView>
 </ContentPage>
 </DataTemplate>
 </TabbedPage.ItemTemplate>
 </TabbedPage>`

在这段代码中,不会触发listview命令行为。

我不熟悉Prism,但由于EventToCommand位于listview中,因此默认绑定是listview传递的数据,而不是ViewModel

您可以尝试以下方法:

<behaviors:EventToCommandBehavior EventName="ItemTapped"
                         Command="{Binding Source={x:Reference this},Path=BindingContext.ListItemTapped}"
                         EventArgsParameterPath="{Binding .}"/>

并为您的页面命名:

 <TabbedPage .... x:Name="this">

因此,listview中的绑定应该有效地重定向到viewmodel中的命令


我希望这会有所帮助。

我不熟悉Prism,但由于EventToCommand位于ListView中,因此默认绑定是ListView传递的数据,而不是ViewModel

您可以尝试以下方法:

<behaviors:EventToCommandBehavior EventName="ItemTapped"
                         Command="{Binding Source={x:Reference this},Path=BindingContext.ListItemTapped}"
                         EventArgsParameterPath="{Binding .}"/>

并为您的页面命名:

 <TabbedPage .... x:Name="this">

因此,listview中的绑定应该有效地重定向到viewmodel中的命令

我希望这有帮助