uwpprismprism-4

Prism Library UWP Show multiple views for an app


I am trying Show multiple views for an app for Prism Library UWP.

I got System.NullReferenceException for frame.Navigate(typeof(ScreenCapture)); like below:

        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);
        }

How to implement multiple view in Prism library for uwp.


Solution

  • Your project is Xamarin.Forms, but the parameter of frame.Navigate(typeof(ScreenCapture)); is uwp Page type. I checked your code, ScreenCapture is Forms ContentPage. For your requirement, you could use Dependency service to call ExecuteNewWindow and place ExecuteNewWindow in UWP project.

    Interface

    public interface INewPageService
    {
         void  CreateNewPage();
    }
    

    Implement

    [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);
    
             }
        }
    }
    

    Usage

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

    Please don't forget register it in uwp app.xaml.cs file.

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