Example #1
0
        /// <summary>
        /// Launches another window with the specified page type.
        /// </summary>
        /// <param name="viewType">Type of the page requested in the secondary window.</param>
        /// <param name="parameter">Page parameter to pass to the new page instance.</param>
        /// <returns>Awaitable task is returned.</returns>
        public virtual async Task NewWindow(Type viewType, object parameter)
        {
            try
            {
                // Create a new window
                var view     = CoreApplication.CreateNewView();
                int windowID = 0;
                await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    windowID = ApplicationView.GetApplicationViewIdForWindow(CoreWindow.GetForCurrentThread());

                    // Register the new window
                    this.RegisterCoreWindow();

                    // Create a frame for the new window
                    var frame = new ApplicationFrame(true);
                    Window.Current.Content = frame;

                    // Navigate to a page within the new window based on the parameters of this method
                    this.SecondaryWindow(new NavigationRequest(viewType, this.SerializeParameter(parameter)));

                    // Show the new window
                    Window.Current.Activate();
                    ApplicationView.GetForCurrentView().Consolidated += View_Consolidated;

                    PlatformBase.CurrentCore.Logger.Log(LogLevels.Warning, $"Launched new window");
                });

                // Run this on the last dispatcher so the windows get positioned correctly
                bool  viewShown;
                await AppWindows[AppWindows.Keys.First()].Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(windowID);
                });

                // Track the new window
                AppWindows.Add(windowID, view);
            }
            catch (Exception ex)
            {
                PlatformBase.CurrentCore.Logger.LogError(ex, "Could not create new window for view type {0}.", viewType.Name);
                throw ex;
            }
        }
        // Obtain from MS docs
        // https://docs.microsoft.com/en-us/windows/uwp/design/layout/app-window
        private async void NewWindow(String Title, Type Page)
        {
            appWindow = await AppWindow.TryCreateAsync();

            appWindow.Title = Title;
            Frame appWindowContentFrame = new Frame();

            appWindowContentFrame.Navigate(Page);

            ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowContentFrame);

            AppWindows.Add(appWindowContentFrame.UIContext, appWindow);

            appWindow.Closed += delegate
            {
                MainPage.AppWindows.Remove(appWindowContentFrame.UIContext);
                appWindowContentFrame.Content = null;
                appWindow = null;
            };

            await appWindow.TryShowAsync();
        }