Prism Library UWP 显示一个应用的多个视图

Prism Library UWP Show multiple views for an app

我正在尝试 Show multiple views for an app 棱镜库 UWP。

我得到 System.NullReferenceException frame.Navigate(typeof(ScreenCapture)); 如下所示:

        async void ExecuteNewWindow()
        {
            CoreApplicationView newView = CoreApplication.CreateNewView();
            int newViewId = 0;
            await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Frame frame = new Frame();
                frame.Navigate(typeof(ScreenCapture));
                Window.Current.Content = frame;
                // You have to activate the window in order to show it later.
                Window.Current.Activate();

                newViewId = ApplicationView.GetForCurrentView().Id;
            });
            bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
        }

如何在 uwp 的 Prism 库中实现多视图。

你的项目是Xamarin.Forms,但是frame.Navigate(typeof(ScreenCapture));的参数是uwp页面类型。我检查了你的代码,ScreenCapture 是表格 ContentPage。根据您的要求,您可以使用 Dependency service 调用 ExecuteNewWindow 并在 UWP 项目中放置 ExecuteNewWindow

界面

public interface INewPageService
{
     void  CreateNewPage();
}

实施

[assembly: Dependency(typeof(INewPageService))]

namespace BlankApp1.UWP
{
   public class INewPageServiceImplement : INewPageService
    {
        public async void CreateNewPage()
        {
            CoreApplicationView newView = CoreApplication.CreateNewView();
            int newViewId = 0;
            await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Frame frame = new Frame();
                frame.Navigate(typeof(NewPage)); // uwp page
                Window.Current.Content = frame;
                // You have to activate the window in order to show it later.
                Window.Current.Activate();

                newViewId = ApplicationView.GetForCurrentView().Id;
            });
            bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);

         }
    }
}

用法

DependencyService.Get<INewPageService>(DependencyFetchTarget.NewInstance).CreateNewPage();

请不要忘记在 uwp app.xaml.cs 文件中注册它。

if (rootFrame == null)
{
    // Create a Frame to act as the navigation context and navigate to the first page
    rootFrame = new Frame();

    rootFrame.NavigationFailed += OnNavigationFailed;

    Xamarin.Forms.Forms.Init(e);
    DependencyService.Register<INewPageService, INewPageServiceImplement>();
    if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
    {
        //TODO: Load state from previously suspended application
    }

    // Place the frame in the current Window
    Window.Current.Content = rootFrame;
}