C# Winrt MVVMLight在ListItem上导航使用参数单击
Iam使用适用于windows 8.1应用程序的MVVMlight。我想在单击列表项时导航到新视图,并将单击的项作为参数传递 我定义了一个附加属性,如:C# Winrt MVVMLight在ListItem上导航使用参数单击,c#,windows-runtime,winrt-xaml,mvvm-light,C#,Windows Runtime,Winrt Xaml,Mvvm Light,Iam使用适用于windows 8.1应用程序的MVVMlight。我想在单击列表项时导航到新视图,并将单击的项作为参数传递 我定义了一个附加属性,如: public static class ItemClickCommand { public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand
public static class ItemClickCommand
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand),
typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged));
public static void SetCommand(DependencyObject d, ICommand value)
{
d.SetValue(CommandProperty, value);
}
public static ICommand GetCommand(DependencyObject d)
{
return (ICommand)d.GetValue(CommandProperty);
}
private static void OnCommandPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var control = d as ListViewBase;
if (control != null)
control.ItemClick += OnItemClick;
}
private static void OnItemClick(object sender, ItemClickEventArgs e)
{
var control = sender as ListViewBase;
var command = GetCommand(control);
if (command != null && command.CanExecute(e.ClickedItem))
command.Execute(e.ClickedItem);
}
}
视图中的xaml如下所示:
<GridView
ItemsSource="{Binding Source={StaticResource ItemsSource}}"
ItemTemplate="{StaticResource itemsTemplate}"
SelectionMode="None"
helpers:ItemClickCommand.Command="{Binding ItemClicked}"
>
</GridView>
以及视图模型:
private RelayCommand<Item> _ItemClicked;
public RelayCommand<Item> ItemClicked
{
get
{
if (_ItemClicked == null)
{
_ItemClicked = new RelayCommand<Item>(
(item) =>
{
_navigationService.Navigate(typeof(ItemsPage));
});
}
return _ItemClicked;
}
}
private RelayCommand\u项目点击;
已单击公共中继命令项
{
得到
{
如果(_ItemClicked==null)
{
_ItemClicked=新建RelayCommand(
(项目)=>
{
_导航服务。导航(typeof(ItemsPage));
});
}
返回已单击的项目;
}
}
当我单击网格项时,什么也没有发生。我求助于本教程作者:Laurent Bugnon
调试时,是否调用
RelayCommand
?有输出吗?如果导航失败,它将自动失败(并返回False
)。单击项目时RelayCommand未运行,我通过添加IsItemClickEnabled=“True”修复了它