Windows模板Studio Prism导航

问题描述 投票:1回答:1

我正在使用Windows Template Studio(Prism)创建一个测试项目,但从文档中,我无法弄清楚导航。我现在知道如何使用MVVM Light,但不得不问,因为文档没有详细说明。如何在Prism Windows Template Studio中从一个页面导航到另一个页面?

适用于MVVM Light:

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

适用于Template10:

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

c# uwp prism windows-template-studio
1个回答
2
投票

您可以使用Windows Template Studio创建测试应用程序并选中导航页面项目类型,然后检查棱镜设计模式。你会在_navigationService班找到ShellViewModel

适用于Windows Template 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;
    }
}

_navigationService来自ShellViewModel构造方法。这个实例是在App类中创建的。

protected override UIElement CreateShell(Frame rootFrame)
{
    var shell = Container.Resolve<ShellPage>();
    shell.SetRootFrame(rootFrame);
    return shell;
}
© www.soinside.com 2019 - 2024. All rights reserved.