Beispiel #1
0
        /// <summary>
        /// Method executes as application entry point - that is -
        /// this bit of code executes before anything else in this
        /// class and application.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            try
            {
                // Set shutdown mode here (and reset further below) to enable showing custom dialogs (messageboxes)
                // durring start-up without shutting down application when the custom dialogs (messagebox) closes
                ShutdownMode = ShutdownMode.OnExplicitShutdown;
            }
            catch
            {
            }

            IOptions              options         = null;
            ISettingsManager      settingsManager = null;
            IThemesManager        themesManager   = null;
            IApplicationViewModel AppViewModel    = null;

            try
            {
                // Install core components and add optionals after load of session data
                // since some of them require setup parameters in constructor
                // (TODO FIXME Add Initializesession method in registerted ExplorerViewModel tool)
                _Container = new WindsorContainer();
                Installers.InstallWindsorCore(_Container);

                // Resolve SettingsManager to retrieve app settings/session data
                // and start with correct parameters from last session (theme, window pos etc...)
                settingsManager = _Container.Resolve <ISettingsManager>();
                themesManager   = _Container.Resolve <IThemesManager>();
                _AppCore        = _Container.Resolve <IAppCore>();

                var task = Task.Run(async() =>  // Off Loading Load Programm Settings to non-UI thread
                {
                    options = await settingsManager.LoadOptionsAsync(_AppCore.DirFileAppSettingsData, themesManager);
                });
                task.Wait(); // Block this to ensure that results are usable in next steps of sequence

                AppViewModel = _Container.Resolve <IApplicationViewModel>();
                var task1 = Task.Run(async() =>  // Off Loading Load Programm Settings to non-UI thread
                {
                    await AppViewModel.LoadConfigOnAppStartupAsync(options, settingsManager, themesManager);
                });
                task1.Wait(); // Block this to ensure that results are usable in next steps of sequence

                // This allows castle to look at the current assembly and look for implementations
                // of the IWindsorInstaller interface
                _Container.Install(FromAssembly.This());                         // Register

                Thread.CurrentThread.CurrentCulture   = new CultureInfo(options.LanguageSelected);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(options.LanguageSelected);

                if (options.RunSingleInstance == true)
                {
                    if (enforcer.ShouldApplicationExit() == true)
                    {
                        if (AppIsShuttingDown == false)
                        {
                            AppIsShuttingDown = true;
                            Shutdown();
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                Logger.Error(exp);
                Console.WriteLine("");
                Console.WriteLine("Unexpected Error 1 in App.Application_Startup()");
                Console.WriteLine("   Message:{0}", exp.Message);
                Console.WriteLine("StackTrace:{0}", exp.StackTrace);
                Console.WriteLine("");
            }

            var start = _Container.Resolve <IShell <MainWindow> >();     // Resolve

            //resolve our shell to start the application.
            if (start != null)
            {
                // Configure Explorer Settings from last session


                start.ConfigureSession(AppViewModel, settingsManager);
                AppViewModel.EnableMainWindowActivated(true);

/////                var toolWindowRegistry = _Container.Resolve<IToolWindowRegistry>();
/////                toolWindowRegistry.PublishTools();

                if (this.AppIsShuttingDown == false)
                {
                    this.ShutdownMode = ShutdownMode.OnLastWindowClose;
                }
            }
            else
            {
                throw new Exception("Main Window construction failed in application boot strapper class.");
            }

            // Show the startpage if application starts for the very first time
            // (This requires that command binding was succesfully done before this line)
            if (string.IsNullOrEmpty(settingsManager.SessionData.LastActiveFile))
            {
                AppViewModel.ShowStartPage();
            }

            start.Run();                                              // Show the mainWindow

            var msgBox = _Container.Resolve <IMessageBoxService>();

            // discover via Plugin folder instead
            MiniUML.Model.MiniUmlPluginLoader.LoadPlugins(
                _AppCore.AssemblyEntryLocation + @".\Plugins\MiniUML.Plugins.UmlClassDiagram\",
                AppViewModel, msgBox);


            if (e != null)
            {
                ProcessCmdLine(e.Args, AppViewModel);
            }

            // Modules (and everything else) have been initialized if we got here
            var output = _Container.Resolve <IMessageManager>();

            output.Output.AppendLine("Get involved at: https://github.com/Dirkster99/Edi");

            _AppCore.CreateAppDataFolder();

// Cannot set shutdown mode when application is already shuttong down
//            try
//            {
//                if (AppIsShuttingDown == false)
//                    ShutdownMode = ShutdownMode.OnExplicitShutdown;
//            }
//            catch
//            {
//            }

            // 1) Application hangs when this is set to null while MainWindow is visible
            // 2) Application throws exception when this is set as owner of window when it
            //    was never visible.
            //
            if (Current.MainWindow != null)
            {
                if (Current.MainWindow.IsVisible == false)
                {
                    Current.MainWindow = null;
                }
            }

/////            if (AppIsShuttingDown == false)
/////                Shutdown();
        }