我正在尝试弄清楚如何绑定(bind) ListView 的 ItemTapped 以使用 Prism 导航详细信息页面。 我尝试使用 DelegateCommand 但出现错误:
Exception is: XamlParseException - Position 15:7. No Property of name ItemTapped found
查看:
<ListView
ItemsSource="{Binding UsersList}"
SelectedItem="{Binding SelectedUser}"
ItemTapped="{Binding ShowUserDetail}"
RowHeight="65" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="20" HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}" TextColor="Blue" FontSize="15"/>
<Label Text="{Binding Email}" TextColor="Gray" FontSize="11"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
View 模型:
public class UsersViewModel : BindableBase
{
..... some bindable objects
INavigationService _navigationService;
public DelegateCommand ShowUserDetail { get; set; }
public UsersViewModel (INavigationService navigationService)
{
_navigationService = navigationService;
ShowUserDetail = new DelegateCommand(OnShowUserDetail);
}
public void OnShowUserDetail()
{
var par = new NavigationParameters();
par.Add("user", SelectedUser);
_navigationService.Navigate("UserDetail", par);
}
....
当我将此 DelegateCommand 绑定(bind)到 <Button>
时导航工程。
可能这与 Prism 无关,但我找不到任何使用它的示例。
谢谢。
最佳答案
Button 有一个要绑定(bind)的 Command 属性,而 ListView 的 ItemTapped 是一个需要事件处理程序的事件。如果你想使用绑定(bind),你必须使用一个行为:
<ListView
ItemsSource="{Binding UsersList}"
SelectedItem="{Binding SelectedUser}"
RowHeight="65" >
<ListView.Behaviors>
<b:EventToCommand EventName="ItemTapped" Command="{Binding ShowUserDetail}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="20" HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}" TextColor="Blue" FontSize="15"/>
<Label Text="{Binding Email}" TextColor="Gray" FontSize="11"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
来源:https://forums.xamarin.com/discussion/comment/180600/#Comment_180600
编辑: EventToCommand是 this NuGet package 中的一种行为.
关于listview - 如何在 ViewModel 中绑定(bind) ListView 的 ItemTapped 属性以导航详细信息页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36600193/