C# Windows模板工作室棱镜导航

C# Windows模板工作室棱镜导航,c#,uwp,prism,windows-template-studio,C#,Uwp,Prism,Windows Template Studio,我正在使用WindowsTemplateStudio(Prism)创建一个测试项目,但是从文档中,我无法理解导航。我现在知道如何使用MVVM Light来实现这一点,但我不得不问,因为文档并没有详细说明。如何在Prism Windows Template Studio中从一页导航到另一页 适用于MVVM灯光: ViewModelLocator.Current.NavigationService.Navigate(typeof(MyViewModel).FullName,null) 在Templa

我正在使用WindowsTemplateStudio(Prism)创建一个测试项目,但是从文档中,我无法理解导航。我现在知道如何使用MVVM Light来实现这一点,但我不得不问,因为文档并没有详细说明。如何在Prism Windows Template Studio中从一页导航到另一页

适用于MVVM灯光:

ViewModelLocator.Current.NavigationService.Navigate(typeof(MyViewModel).FullName,null)

在Template10上工作:


BootStrapper.Current.NavigationService.Navigate(typeof(MyViewPage),null)

您可以使用Windows Template Studio创建测试应用程序,检查导航页面项目类型,然后检查prism设计模式。您将在
ShellViewModel
类中找到
\u navigationService

protected override UIElement CreateShell(Frame rootFrame)
{
    var shell = Container.Resolve<ShellPage>();
    shell.SetRootFrame(rootFrame);
    return shell;
}
在Windows模板Studio Prism上工作

_navigationService.Navigate(pageKey, null);
ShellViewModel.cs

public class ShellViewModel : ViewModelBase
{
    private static INavigationService _navigationService;
    private WinUI.NavigationView _navigationView;
    private bool _isBackEnabled;
    private WinUI.NavigationViewItem _selected;

    public ICommand ItemInvokedCommand { get; }

    public bool IsBackEnabled
    {
        get { return _isBackEnabled; }
        set { SetProperty(ref _isBackEnabled, value); }
    }

    public WinUI.NavigationViewItem Selected
    {
        get { return _selected; }
        set { SetProperty(ref _selected, value); }
    }

    public ShellViewModel(INavigationService navigationServiceInstance)
    {
        _navigationService = navigationServiceInstance;
        ItemInvokedCommand = new DelegateCommand<WinUI.NavigationViewItemInvokedEventArgs>(OnItemInvoked);
    }

    public void Initialize(Frame frame, WinUI.NavigationView navigationView)
    {
        _navigationView = navigationView;
        frame.NavigationFailed += (sender, e) =>
        {
            throw e.Exception;
        };
        frame.Navigated += Frame_Navigated;
        _navigationView.BackRequested += OnBackRequested;
    }

    private void OnItemInvoked(WinUI.NavigationViewItemInvokedEventArgs args)
    {
        var item = _navigationView.MenuItems
                        .OfType<WinUI.NavigationViewItem>()
                        .First(menuItem => (string)menuItem.Content == (string)args.InvokedItem);
        var pageKey = item.GetValue(NavHelper.NavigateToProperty) as string;
        _navigationService.Navigate(pageKey, null);


    }

    private void Frame_Navigated(object sender, NavigationEventArgs e)
    {
        IsBackEnabled = _navigationService.CanGoBack();
        Selected = _navigationView.MenuItems
                        .OfType<WinUI.NavigationViewItem>()
                        .FirstOrDefault(menuItem => IsMenuItemForPageType(menuItem, e.SourcePageType));
    }

    private void OnBackRequested(WinUI.NavigationView sender, WinUI.NavigationViewBackRequestedEventArgs args)
    {
        _navigationService.GoBack();
    }

    private bool IsMenuItemForPageType(WinUI.NavigationViewItem menuItem, Type sourcePageType)
    {
        var sourcePageKey = sourcePageType.Name;
        sourcePageKey = sourcePageKey.Substring(0, sourcePageKey.Length - 4);
        var pageKey = menuItem.GetValue(NavHelper.NavigateToProperty) as string;
        return pageKey == sourcePageKey;
    }
}

因此,允许
\u导航服务.Navigate(pageKey,null)可访问性在其他页面上,我需要在
ShellViewModel
上将
\u navigationService
设置为
public
?我只是尝试了一下,确实奏效了,但是有什么原因,为什么我不应该这样做呢?你不需要将_navigationService设置为public,你可以在每个视图模型构造方法中获得navigationServiceInstance。例如,
publicblank1viewmodel(INavigationService-navigationServiceInstance){{u-navigationService=navigationServiceInstance;}
App.xaml.cs的情况如何?当导航到页面时,什么是好的做法?您可以参考Tempate 10 Prism。我认为与BootStrapper共享navigationService是一种很好的做法。不客气,我认为您也可以将
navigationService
存储在app.xaml.cs中,就像模板10
BootStrapper
类一样。如果答案是有用的,请考虑接受它。谢谢。