コード例 #1
0
        /// <summary>
        /// Creates a new window and displays it. It shows the window as a dialog and waits till the window has closed.
        /// </summary>
        /// <typeparam name="TWindow">The type of the window that is to be created.</typeparam>
        /// <param name="parameters">The parameters that are passed to the view model of the window.</param>
        /// <param name="isMainWindow">Determines whether the new window is set as the main window of the application. If the main window is closed, then the application is shut down.</param>
        /// <exception cref="InvalidOperationException">If the the window or it's view model can not be instantiated, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns whether the navigation was successful or cancelled.</returns>
        public async Task <WindowNavigationResult> NavigateDialogAsync <TWindow>(object parameters) where TWindow : Window
        {
            // Creates the new window
            WindowCreationResult result = await this.CreateWindowAsync <TWindow>(parameters);

            // Checks if the window could be created
            if (result.NavigationResult == NavigationResult.Canceled)
            {
                return new WindowNavigationResult {
                           Result = NavigationResult.Canceled
                }
            }
            ;

            // Creates and adds the new navigation manager to the list of navigation managers
            this.navigationServices.Add(result.NavigationService);
            if (this.WindowCreated != null)
            {
                this.WindowCreated(this, new WindowEventArgs(result.NavigationService));
            }

            // Opens the new window
            result.Window.ShowDialog();

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(new WindowNavigationResult
            {
                Result = NavigationResult.Navigated,
                NavigationService = result.NavigationService
            });
        }
コード例 #2
0
        /// <summary>
        /// Creates a new window, displays it, and navigates to a view within the window. It shows the window as a dialog and waits till the window has closed.
        /// </summary>
        /// <typeparam name="TWindow">The type of the window that is to be created.</typeparam>
        /// <typeparam name="TView">The type of the view to which is navigated within the window.</typeparam>
        /// <param name="windowParameters">The parameters that are passed to the view model of the window.</param>
        /// <param name="viewParameters">The parameters that are passeed to the view model of the view.</param>
        /// <exception cref="InvalidOperationException">If the view, the window, or either of the view models can not be instantiated, or the window does not support navigation, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns whether the navigation was successful or cancelled.</returns>
        public async Task <WindowNavigationResult> NavigateDialogAsync <TWindow, TView>(object windowParameters, object viewParameters)
            where TWindow : Window
            where TView : Page
        {
            // Creates the new window
            WindowCreationResult result = await this.CreateWindowAsync <TWindow>(windowParameters);

            // Checks if the window could be created
            if (result.NavigationResult == NavigationResult.Canceled)
            {
                return new WindowNavigationResult {
                           Result = NavigationResult.Canceled
                }
            }
            ;

            // Checks if the window supports navigation
            if (!result.NavigationService.SupportsNavigation)
            {
                // Since the window does not support navigation, the window view model is deactivated and disposed of
                await result.ViewModel.OnDeactivateAsync();

                result.ViewModel.Dispose();
                throw new InvalidOperationException(Resources.Localization.WindowNavigationService.NavigationNotSupportedExceptionMessage);
            }

            // Navigates to the specified view
            if (await result.NavigationService.NavigateAsync <TView>(viewParameters) == NavigationResult.Canceled)
            {
                // Since the view could not be navigated to, the new window view model is deactivated, disposed of, and the navigation is aborted
                await result.ViewModel.OnDeactivateAsync();

                result.ViewModel.Dispose();
                return(new WindowNavigationResult {
                    Result = NavigationResult.Canceled
                });
            }

            // Adds the new navigation service to the list of navigation services
            this.navigationServices.Add(result.NavigationService);
            if (this.WindowCreated != null)
            {
                this.WindowCreated(this, new WindowEventArgs(result.NavigationService));
            }

            // Opens the new window
            result.Window.ShowDialog();

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(new WindowNavigationResult
            {
                Result = NavigationResult.Navigated,
                NavigationService = result.NavigationService
            });
        }
コード例 #3
0
        /// <summary>
        /// Creates a new window and displays it.
        /// </summary>
        /// <typeparam name="TWindow">The type of the window that is to be created.</typeparam>
        /// <param name="parameters">The parameters that are passed to the view model of the window.</param>
        /// <param name="isMainWindow">Determines whether the new window is set as the main window of the application. If the main window is closed, then the application is shut down.</param>
        /// <exception cref="InvalidOperationException">If the the window or it's view model can not be instantiated, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns whether the navigation was successful or cancelled.</returns>
        public async Task <WindowNavigationResult> NavigateAsync <TWindow>(object parameters, bool isMainWindow) where TWindow : Window
        {
            // Creates the new window
            WindowCreationResult result = await this.CreateWindowAsync <TWindow>(parameters);

            // Checks if the window could be created
            if (result.NavigationResult == NavigationResult.Canceled)
            {
                return new WindowNavigationResult {
                           Result = NavigationResult.Canceled
                }
            }
            ;

            // Creates and adds the new navigation manager to the list of navigation managers
            this.navigationServices.Add(result.NavigationService);
            if (this.WindowCreated != null)
            {
                this.WindowCreated(this, new WindowEventArgs(result.NavigationService));
            }

            // Sets the window as the new main window, if the user requested us to do so
            if (Application.Current != null)
            {
                if (isMainWindow)
                {
                    Application.Current.MainWindow = result.Window;
                }
                else if (Application.Current.MainWindow != null)
                {
                    result.Window.Owner = Application.Current.MainWindow;
                }
            }

            // Opens the new window
            result.Window.Show();

            // Sets the ownership of all opened windows
            if (isMainWindow)
            {
                foreach (Window childWindow in this.navigationServices.Select(navigationService => navigationService.Window).Where(childWindow => childWindow != result.Window).ToList())
                {
                    childWindow.Owner = result.Window;
                }
            }

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(new WindowNavigationResult
            {
                Result = NavigationResult.Navigated,
                NavigationService = result.NavigationService
            });
        }
コード例 #4
0
        /// <summary>
        /// Creates a new window.
        /// </summary>
        /// <param name="anchorViewId">The ID of the anchor window of the new window.</param>
        /// <param name="anchorWindowSizePreference">The preferred size for the anchor window.</param>
        /// <param name="newWindowSizePreference">The preferred size for the new window.</param>
        /// <returns>Returns a window creation result, which contains the a flag that determines whether the creation was successful and the navigation service, for the newly created window.</returns>
        private async Task <WindowCreationResult> CreateWindowAsync(Nullable <int> anchorViewId, ViewSizePreference anchorWindowSizePreference, ViewSizePreference newWindowSizePreference)
        {
            // Creates the result of the window creation a new navigation service for the window
            WindowCreationResult windowCreationResult = new WindowCreationResult();

            // Checks if this is the first creation of a new window (which is when there are no navigation services, yet), if so then the default window, which is created at application startup, is taken, otherwise a new window is created
            CoreApplicationView newWindow;

            if (this.navigationServices.Any())
            {
                newWindow = CoreApplication.CreateNewView();
            }
            else
            {
                newWindow = CoreApplication.GetCurrentView();
            }

            // Actually creates the new window and initializes it on its dispatcher
            await newWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                // Creates the navigation frame and the navigation service, which are needed to navigate within the newly created window
                Window.Current.Content = new Frame();
                windowCreationResult.NavigationService = new NavigationService(this.iocContainer, Window.Current, ApplicationView.GetForCurrentView(), Window.Current.Content as Frame);

                // Signs up for the closed event of the window, when the closed event is raised, then the life-cycle methods of the view model is called and the the window is disposed of
                Window.Current.Closed += async(sender, e) => await this.CloseWindowAsync(windowCreationResult.NavigationService);

                // Makes sure that the window is properly activated
                Window.Current.Activate();
            });

            // Shows the window and determines whether the creation was successful (but the window only needs to shown, if this is not the first navigation, because the default window, that is created at application startup, is already open)
            if (this.navigationServices.Any())
            {
                if (anchorViewId.HasValue)
                {
                    windowCreationResult.WasSuccessful = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(windowCreationResult.NavigationService.ApplicationView.Id, newWindowSizePreference, anchorViewId.Value, anchorWindowSizePreference);
                }
                else
                {
                    windowCreationResult.WasSuccessful = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(windowCreationResult.NavigationService.ApplicationView.Id, newWindowSizePreference);
                }
            }
            else
            {
                windowCreationResult.WasSuccessful = true;
            }

            // Returns the result
            return(windowCreationResult);
        }
コード例 #5
0
        /// <summary>
        /// Creates a new window and navigates to the specified view.
        /// </summary>
        /// <typeparam name="TView">The type of the view to which the user should be navigated in the new window.</typeparam>
        /// <param name="parameters">The parameters, which are injected in to the view model of the view.</param>
        /// <param name="newWindowSizePreference">The size preference for the new window, which determines how the new window is arranged in respect to the anchor window.</param>
        /// <param name="anchorWindowNavigationService">The navigation service of the window, which is the anchor for the new window. This can be used to specify how the two windows are being arranged by Windows.</param>
        /// <param name="anchorWindowSizePreference">The size preference for the anchor window, which determines how the anchor window is arranged in respect to the new window.</param>
        /// <returns>Returns the result of the navigation.</returns>
        public async Task <WindowNavigationResult> NavigateAsync <TView>(object parameters, ViewSizePreference newWindowSizePreference, NavigationService anchorWindowNavigationService, ViewSizePreference anchorWindowSizePreference) where TView : Page
        {
            // Creates the new window
            WindowCreationResult result = await this.CreateWindowAsync(anchorWindowNavigationService?.ApplicationView.Id, anchorWindowSizePreference, newWindowSizePreference);

            // Checks if the window could be created, if not then the navigation can not be performed
            if (!result.WasSuccessful)
            {
                return new WindowNavigationResult {
                           Result = NavigationResult.Canceled
                }
            }
            ;

            // Navigates to the specified view
            if (await result.NavigationService.NavigateAsync <TView>(parameters) == NavigationResult.Canceled)
            {
                // Since the view could not be navigated to, the new window is closed, and the navigation is aborted
                await result.NavigationService.CloseWindowAsync(false, NavigationReason.WindowClosed);

                return(new WindowNavigationResult {
                    Result = NavigationResult.Canceled
                });
            }

            // Adds the new navigation service to the list of navigation services and invokes the window created event
            this.navigationServices.Add(result.NavigationService);
            this.WindowCreated?.Invoke(this, new WindowEventArgs(result.NavigationService));

            // Since the navigation was successful, the navigation service is returned
            return(new WindowNavigationResult
            {
                Result = NavigationResult.Navigated,
                NavigationService = result.NavigationService
            });
        }
コード例 #6
0
        /// <summary>
        /// Creates a new window, displays it, and navigates to a view within the window.
        /// </summary>
        /// <typeparam name="TWindow">The type of the window that is to be created.</typeparam>
        /// <typeparam name="TView">The type of the view to which is navigated within the window.</typeparam>
        /// <param name="windowParameters">The parameters that are passed to the view model of the window.</param>
        /// <param name="viewParameters">The parameters that are passeed to the view model of the view.</param>
        /// <param name="isMainWindow">Determines whether the new window is set as the main window of the application. If the main window is closed, the application is shut down.</param>
        /// <exception cref="InvalidOperationException">If the view, the window, or either of the view models can not be instantiated, or the window does not support navigation, an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns whether the navigation was successful or cancelled.</returns>
        public async Task <WindowNavigationResult> NavigateAsync <TWindow, TView>(object windowParameters, object viewParameters, bool isMainWindow = false)
            where TWindow : Window
            where TView : Page
        {
            // Creates the new window
            WindowCreationResult result = await this.CreateWindowAsync <TWindow>(windowParameters);

            // Checks if the window could be created
            if (result.NavigationResult == NavigationResult.Canceled)
            {
                return new WindowNavigationResult {
                           Result = NavigationResult.Canceled
                }
            }
            ;

            // Checks if the window supports navigation
            if (!result.NavigationService.SupportsNavigation)
            {
                // Since the window does not support navigation, the window view model is deactivated and disposed of
                await result.ViewModel.OnDeactivateAsync();

                result.ViewModel.Dispose();
                throw new InvalidOperationException(Resources.Localization.WindowNavigationService.NavigationNotSupportedExceptionMessage);
            }

            // Navigates to the specified view
            if (await result.NavigationService.NavigateAsync <TView>(viewParameters) == NavigationResult.Canceled)
            {
                // Since the view could not be navigated to, the new window view model is deactivated, disposed of, and the navigation is aborted
                await result.ViewModel.OnDeactivateAsync();

                result.ViewModel.Dispose();
                return(new WindowNavigationResult {
                    Result = NavigationResult.Canceled
                });
            }

            // Adds the new navigation service to the list of navigation services
            this.navigationServices.Add(result.NavigationService);
            if (this.WindowCreated != null)
            {
                this.WindowCreated(this, new WindowEventArgs(result.NavigationService));
            }

            // Sets the window as the new main window, if the user requested us to do so
            if (Application.Current != null)
            {
                if (isMainWindow)
                {
                    Application.Current.MainWindow = result.Window;
                }
                else if (Application.Current.MainWindow != null && result.Window != Application.Current.MainWindow)
                {
                    result.Window.Owner = Application.Current.MainWindow;
                }
            }

            // Opens the new window
            result.Window.Show();

            // Sets the ownership of all opened windows
            if (isMainWindow)
            {
                foreach (Window childWindow in this.navigationServices.Select(navigationService => navigationService.Window).Where(childWindow => childWindow != result.Window).ToList())
                {
                    childWindow.Owner = result.Window;
                }
            }

            // Since the navigation was successful, Navigated is returned as a result of the navigation
            return(new WindowNavigationResult
            {
                Result = NavigationResult.Navigated,
                NavigationService = result.NavigationService
            });
        }