2013-08-18 42 views
3

我開始在我的應用程序中實現MVVM,並且知道用戶何時導航到視圖。WP MVVM導航OnNavigatedTO

要在視圖之間導航,我可以只使用navigationService.Navigate(...);

如何檢查,當我瀏覽到的觀點? 我可以使用活動navigationService.Navigated

有沒有其他方法我可以使用像OnNavigatedTo頁面本身提供的?

回答

0

謝謝你提供的答案。兩者在一段時間內都很有幫助,直到我決定創建由少數人創建的導航服務的自定義實現爲止。 然後,我對Cimbalino工具包做出了貢獻,以表明這一點,並且它已經引入了一段時間。

我個人認爲,解決我的問題最好。看看那裏的導航服務。 Navigated事件幾乎解決了我的問題。

https://github.com/Cimbalino/Cimbalino-Toolkit

它基本上可以歸結爲這(在視圖模型):

_navigationService.Navigated += OnNavigated; 
1

XAML:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71" 

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
DataContext="{Binding titleSearchViewModel, Source={StaticResource Locator}}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger> 
      <cmd:EventToCommand Command="{Binding PageLoaded, Mode=OneWay}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 

VM:

private RelayCommand _PageLoaded; 
public RelayCommand PageLoaded 
     { 
      get 
      { 
       if (_PageLoaded == null) 
       { 
        _PageLoaded = new RelayCommand(
            () => Loaded() 
         ); 
       } 
       return _PageLoaded; 
      } 
     } 
1

如果這個問題仍然是實際的,我更喜歡這種解決辦法:http://www.geoffhudik.com/tech/2010/10/10/another-wp7-navigation-approach-with-mvvm.html

如果要使用它,它可以發送來自發件人ViewModel的收件人ViewModel的參數:

SendNavigationMessage(Settings.NAVIGATION_PRODUCTS_SUBCATEGORIES, 
    new Dictionary<string, object> { { "SelectedIndex", Int32.Parse(item.id) } }); 

和接收器應該在XAML定義:

NavigatedToCommand="{Binding RefreshCommand}" 

然後在接收器視圖模型:

public ICommand RefreshCommand // Should be set as NavigatedToCommand="{Binding RefreshCommand}" in xaml 
{ 
    get { return new RelayCommand(Refresh); } 
} 

public void Refresh() 
{  
    _dataService.GetList(SelectedIndex, DownloadedCallback); // So, this would be called automatically after navigating is complete. SelectedIndex is updated at this moment. 
}