【问题标题】:UWP Template10 NavigationService in a Static Method静态方法中的 UWP Template10 NavigationService
【发布时间】:2016-08-25 17:49:59
【问题描述】:

我是使用 Template10 的新手,我正在尝试创建一种在页面之间导航的方法,但在 Template10 中,NavigationService 仅适用于没有静态方法,如何使用 Template10 的 NavigationService 的最佳方式。

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

using Template10.Mvvm;

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

感谢任何帮助。

【问题讨论】:

    标签: uwp template10


    【解决方案1】:

    但后来我无法在其他页面中使用。

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      相关资源
      最近更新 更多