Search code examples
uwptemplate10

UWP Template10 NavigationService in a Static Method


I am new using Template10, and I am trying create a method to navigate between pages, but in Template10 the NavigationService work only on no static methods, how is the best way to use the NavigationService of Template10.

Here is my code, as you can see it show error, if static word is removed it give no errors but then I could not use in other pages.

using Template10.Mvvm;

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

Any help is appreciated.


Solution

  • but then I could not use in other pages.

    You can use this method in other pages by create a new instance of your NavigationUniversalService.

    For example, in my MainPageViewModel I used NavigationService like this:

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

    Then in other page's viewmodel, you can call this method like this:

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

    Problem is, if you want to navigate through NavigationService, you can inherit your class from ViewModelBase, then you can directly use NavigationService to navigate, there is no need to call this NavigationService from other class.

    What I mean is for example like this:

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