静态方法中的 UWP Template10 NavigationService

UWP Template10 NavigationService in a Static Method

我是使用 Template10 的新手,我正在尝试创建一种在页面之间导航的方法,但在 Template10 中,NavigationService 仅在没有静态方法的情况下工作,如何使用 Template10 的 NavigationService 是最好的方法。

这是我的代码,如您所见,它显示错误,如果删除静态词,它不会出现错误,但我无法在其他页面中使用。

using Template10.Mvvm;

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

感谢任何帮助。

but then I could not use in other pages.

您可以通过创建 NavigationUniversalService 的新实例在其他页面中使用此方法。

例如,在我的 MainPageViewModel 中,我这样使用 NavigationService

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

然后在其他页面的viewmodel中,可以这样调用这个方法:

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

问题是,如果你想通过 NavigationService 导航,你可以从 ViewModelBase 继承你的 class,然后你可以直接使用 NavigationService 导航,那里不需要从其他 class.

调用此 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!
    }
}