BindCommand WinRT GridView和get selected item
我有一个GridView,它绑定到我的viewmodel上的属性会话。当在GridView中选择一个新项目时,我希望它触发到一个新视图的导航。Sessions属性是SessionViewModel的列表,但它有几个子类,这些子类具有独立的对应视图。我的视图中当前有以下代码:BindCommand WinRT GridView和get selected item,gridview,mvvm,viewmodel,winrt-xaml,reactiveui,Gridview,Mvvm,Viewmodel,Winrt Xaml,Reactiveui,我有一个GridView,它绑定到我的viewmodel上的属性会话。当在GridView中选择一个新项目时,我希望它触发到一个新视图的导航。Sessions属性是SessionViewModel的列表,但它有几个子类,这些子类具有独立的对应视图。我的视图中当前有以下代码: this.BindCommand(ViewModel, x => x.SessionNavigateCommand, x => x.SessionsGridView, "ItemClick"); 这将激发回我的
this.BindCommand(ViewModel, x => x.SessionNavigateCommand, x => x.SessionsGridView, "ItemClick");
这将激发回我的viewmodel上的SessionNavigateCommand,类型为IReactiveCommand。我想订阅如下命令:
SessionNavigateCommand.Subscribe(x => HostScreen.Router.Navigate.Execute(x));
但是事件参数包装了我需要的实际viewmodel,我不想用视图特定的代码污染我的viewmodel。我在基于导航的WPF应用程序中遇到过类似的设计问题。我的解决方案是在ViewModel上定义操作命令,在视图上定义导航命令,或者将全局导航定义为托管所有内容的NavigationWindow上的命令 在某些情况下,例如,当交互同时导致操作和导航时,我使用视图级命令调用ViewModel命令,然后导航以完成操作 我不确定我对这种方法是否完全满意,虽然它成功地实现了我的目标,但我觉得它错了。如果你能找到更好的解决方案,我会很感兴趣的 导致导航的示例按钮
<Button Style="{StaticResource HyperLinkButtonStyle}"
Command="{Binding GoCurrentCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=View:MainWindow, AncestorLevel=1}}"
Content="Details"/>
<Button
Style="{StaticResource HyperLinkButtonStyle}"
Command="{Binding CompleteCommand}"
Content="Complete"/>
public class MainWindow : NavigationWindow
{
...
//This defines when the command can be run
this._completeCommand = new ReactiveCommand(((SolutionViewModel)DataContext).CompleteCommand.CanExecuteObservable);
//This defines what to do when the command is run
this._completeCommand.Subscribe(
_ =>
{
((SolutionViewModel)DataContext).CompleteCommand.Execute(null);
this.NavigationService.Navigate(new IntentionsListPage { DataContext = this.DataContext });
});