2013-07-02 22 views
0

我有一個GridView綁定到我的viewmodel屬性會話。當在GridView中選擇一個新項目時,我希望這會觸發導航到新視圖。 Sessions屬性是SessionViewModel的列表,但它有幾個具有獨立相應視圖的子類。目前在我的背後視圖代碼我有這樣的:BindCommand WinRT GridView並獲取選定的項目

this.BindCommand(ViewModel, x => x.SessionNavigateCommand, x => x.SessionsGridView, "ItemClick"); 

這觸發回SessionNavigateCommand我的視圖模型,這是類型IReactiveCommand的。我想訂閱的命令,像這樣:

SessionNavigateCommand.Subscribe(x => HostScreen.Router.Navigate.Execute(x)); 

但事件參數包裹實際上視圖模型,我需要,我不希望我的污染與視圖特定代碼視圖模型。

回答

0

我在基於導航的WPF應用程序中遇到了類似的設計問題。我的解決方案是在View上的ViewModel和Navigation命令上定義Action命令,或者在NavigationWindow上定義全局Navigations作爲命令來承載一切。

在某些情況下,例如,交互導致動作和導航,我有我的視圖級別命令調用我的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"/> 

這裏和普通的按鈕

實例按鈕與代表團的示例圖級別命令到ViewModel(注意使用ReactiveUI,所以語法可能不完全符合你的習慣)

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