Windows Template Studio Prism导航

时间:2019-02-21 05:52:10

标签: c# uwp prism windows-template-studio

我正在使用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)

1 个答案:

答案 0 :(得分: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;
}