Winrt MVVMLight navigate on listItemClick with parameters



我正在使用 MVVMlight for Windows 8.1 应用程序。我想在单击列表项时导航到新视图,并将单击的项目作为参数传递。

我已经定义了一个附加属性,例如:

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;
        }        
    }

单击网格项时没有任何反应。

我通过以下方式求助于本教程: Laurent Bugnion

http://msdn.microsoft.com/en-us/magazine/dn237302.aspx

最新更新