Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new shell.
        /// </summary>
        /// <typeparam name="TShell">The type of the shell.</typeparam>
        /// <param name="preInitialize">The pre initialize handler to initialize custom logic. If <c>null</c>, this value will be ignored.</param>
        /// <param name="initializeCommands">The initialize commands handler. If <c>null</c>, no commands will be initialized.</param>
        /// <param name="postInitialize">The post initialize handler to initialize custom logic. If <c>null</c>, this value will be ignored.</param>
        /// <returns>The created shell.</returns>
        /// <exception cref="OrchestraException">The shell is already created and cannot be created again.</exception>
        public TShell Create <TShell>(Action preInitialize = null, Action <ICommandManager> initializeCommands = null, Action postInitialize = null)
            where TShell : IShell
        {
            if (Shell != null)
            {
                Log.ErrorAndThrowException <OrchestraException>("The shell is already created and cannot be created again");
            }

            if (preInitialize != null)
            {
                Log.Debug("Calling pre initialize");

                preInitialize();
            }

            Log.Debug("Creating shell using type '{0}'", typeof(TShell).GetSafeFullName());

            var shell = _typeFactory.CreateInstance <TShell>();

            Shell = shell;

            var shellAsWindow = Shell as Window;

            if (shellAsWindow != null)
            {
                Log.Debug("Setting the new shell as Application.MainWindow");

                var currentApp = Application.Current;
                currentApp.MainWindow = shellAsWindow;
            }

            if (initializeCommands != null)
            {
                Log.Info("Initializing commands");

                initializeCommands(_commandManager);
            }

            Log.Info("Loading keyboard mappings");

            _keyboardMappingsService.Load();

            // Now we have a new window, resubscribe the command manager
            _commandManager.SubscribeToKeyboardEvents();

            if (postInitialize != null)
            {
                Log.Debug("Calling post initialize");

                postInitialize();
            }

            shell.Show();

            return(shell);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new shell.
        /// </summary>
        /// <typeparam name="TShell">The type of the shell.</typeparam>
        /// <param name="postShowShellCallback">The shell created callback.</param>
        /// <returns>The created shell.</returns>
        /// <exception cref="OrchestraException">The shell is already created and cannot be created again.</exception>
        private async Task <TShell> CreateShellInternalAsync <TShell>(Action postShowShellCallback = null)
            where TShell : IShell
        {
            if (Shell != null)
            {
                throw Log.ErrorAndCreateException <OrchestraException>(
                          "The shell is already created and cannot be created again");
            }

            Log.Info("Checking if software was correctly closed previously");

            var shell = default(TShell);
            var successfullyStarted = true;

#if !DEBUG
            try
            {
#endif
            await InitializeBeforeCreatingShellAsync();

            shell = await CreateShellAsync <TShell>();

            _keyboardMappingsService.Load();

            // Now we have a new window, resubscribe the command manager
            _commandManager.SubscribeToKeyboardEvents();

            await InitializeAfterCreatingShellAsync();

            Log.Info("Confirming that application was started successfully");

            await InitializeBeforeShowingShellAsync();

            ShowShell(shell);

            if (postShowShellCallback != null)
            {
                postShowShellCallback();
            }

            await InitializeAfterShowingShellAsync();

#if !DEBUG
        }

        catch (Exception ex)
        {
            App.ReportCrash(ex);
        }
#endif
            return(shell);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new shell.
        /// </summary>
        /// <typeparam name="TShell">The type of the shell.</typeparam>
        /// <param name="postShowShellCallback">The shell created callback.</param>
        /// <returns>The created shell.</returns>
        /// <exception cref="OrchestraException">The shell is already created and cannot be created again.</exception>
        private async Task <TShell> CreateShellInternal <TShell>(Action postShowShellCallback = null)
            where TShell : IShell
        {
            if (Shell != null)
            {
                Log.ErrorAndThrowException <OrchestraException>("The shell is already created and cannot be created again");
            }

            Log.Info("Checking if software was correctly closed previously");

            await _ensureStartupService.EnsureFailSafeStartup();

            await InitializeBeforeCreatingShell();

            await InitializeCommands();

            var shell = await CreateShell <TShell>();

            Log.Info("Loading keyboard mappings");

            _keyboardMappingsService.Load();

            // Now we have a new window, resubscribe the command manager
            _commandManager.SubscribeToKeyboardEvents();

            await InitializeAfterCreatingShell();

            Log.Info("Confirming that application was started successfully");

            _ensureStartupService.ConfirmApplicationStartedSuccessfully();

            await InitializeBeforeShowingShell();

            await ShowShell(shell);

            if (postShowShellCallback != null)
            {
                postShowShellCallback();
            }

            await InitializeAfterShowingShell();

            return(shell);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new shell.
        /// </summary>
        /// <typeparam name="TShell">The type of the shell.</typeparam>
        /// <param name="postShowShellCallback">The shell created callback.</param>
        /// <returns>The created shell.</returns>
        /// <exception cref="OrchestraException">The shell is already created and cannot be created again.</exception>
        private async Task <TShell> CreateShellInternalAsync <TShell>(Action postShowShellCallback = null)
            where TShell : IShell
        {
            if (Shell != null)
            {
                throw Log.ErrorAndCreateException <OrchestraException>("The shell is already created and cannot be created again");
            }

            Log.Info("Checking if software was correctly closed previously");

            await _ensureStartupService.EnsureFailSafeStartupAsync();

            var shell = default(TShell);
            var successfullyStarted = true;

            try
            {
                await InitializeBeforeCreatingShellAsync();

                shell = await CreateShellAsync <TShell>();

                _keyboardMappingsService.Load();

                // Now we have a new window, resubscribe the command manager
                _commandManager.SubscribeToKeyboardEvents();

                await InitializeAfterCreatingShellAsync();

                Log.Info("Confirming that application was started successfully");

                _ensureStartupService.ConfirmApplicationStartedSuccessfully();

                await InitializeBeforeShowingShellAsync();

                ShowShell(shell);

                if (postShowShellCallback != null)
                {
                    postShowShellCallback();
                }

                await InitializeAfterShowingShellAsync();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An unexpected error occurred, shutting down the application");

                successfullyStarted = false;
            }

            if (!successfullyStarted)
            {
                var entryAssembly = Catel.Reflection.AssemblyHelper.GetEntryAssembly();
                var assemblyTitle = entryAssembly.Title();

                // Late resolve so user might change the message service
                var messageService = _dependencyResolver.Resolve <IMessageService>();
                await messageService.ShowErrorAsync(string.Format("An unexpected error occurred while starting {0}. Unfortunately it needs to be closed.\n\nPlease try restarting the application. If this error keeps coming up while starting the application, please contact support.", assemblyTitle), string.Format("Failed to start {0}", assemblyTitle));

                Application.Current.Shutdown(-1);
            }

            return(shell);
        }
        public static Task LoadAsync(this IKeyboardMappingsService keyboardMappingsService)
        {
            Argument.IsNotNull(() => keyboardMappingsService);

            return(TaskHelper.Run(() => keyboardMappingsService.Load(), true));
        }