2016-08-24 57 views
0

我是新的使用Template10,我嘗試創建一個方法來瀏覽頁面,但在Template10中NavigationService只能在沒有靜態方法的情況下工作,如何使用NavigationService的最佳方式是Template10。在靜態方法中的UWP Template10 NavigationService

這是我的代碼,你可以看到它顯示錯誤,如果靜態詞被刪除,它不會給出錯誤,但是我不能在其他頁面中使用。

using Template10.Mvvm; 

namespace Project 
{ 
    class NavigationUniversalService : ViewModelBase 
    {  
     public static void ToCover() 
     { 
      NavigationService.Navigate(typeof(Views.Page_Cover)); 
     }  
    } 
} 

任何幫助表示讚賞。

回答

2

但後來我無法在其他頁面中使用。

您可以通過創建您的NavigationUniversalService的新實例在其他頁面中使用此方法。

例如,在我的MainPageViewModel我用NavigationService這樣的:

public void ToCover() 
{ 
    App.Current.NavigationService.Navigate(typeof(Views.Page_Cover)); 
} 

然後在其他頁面的視圖模型,你可以調用這個方法是這樣的:

MainPageViewModel mainviewmodel = new MainPageViewModel(); 
mainviewmodel.ToCover(); 

問題是,如果你想通過NavigationService導航,您可以從ViewModelBase繼承您的課程,然後您可以直接使用NavigationService進行導航,無需從其他課程中調用此NavigationService

我的意思是比如像這樣:

public class DetailPageViewModel : ViewModelBase 
{ 
    public DetailPageViewModel() 
    { 
     if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) 
     { 
      Value = "Designtime value"; 
     } 
    } 
    ... 
    public void CallMethodInOtherViewModel() 
    { 
     NavigationService.Navigate(typeof(typeof(Views.Page_Cover)); //here! 
    } 
}