Esempio n. 1
0
        protected virtual void RunApplicationInternal(ParagonCommandLineParser cmdLine, IApplicationPackage package, ApplicationMetadata metadata)
        {
            IParagonSplashScreen splash      = null;
            IApplication         application = null;
            Window splashWindow = null;
            var    manifest     = package.Manifest;

#if ENFORCE_PACKAGE_SECURITY
            var isSigned = package.Signature != null;
#endif
            try
            {
                ParagonLogManager.AddApplicationTraceListener(manifest.Id);

                // Load custom WPF theme for the application
                var stylePart   = !string.IsNullOrEmpty(manifest.CustomTheme) ? package.GetPart(manifest.CustomTheme) : null;
                var styleStream = stylePart != null?stylePart.GetStream() : null;

                if (styleStream != null)
                {
                    var theme = XamlReader.Load(styleStream) as ResourceDictionary;
                    if (theme != null)
                    {
#if ENFORCE_PACKAGE_SECURITY
                        if (isSigned)
#endif
                        Application.Current.Resources.MergedDictionaries.Add(theme);
                    }
                }

                // Create and show the splash screen if needed
                if (cmdLine != null && !cmdLine.HasFlag("no-splash") && _createSplashScreen != null)
                {
#if ENFORCE_PACKAGE_SECURITY
                    splash = _createSplashScreen(isSigned ? manifest.Name : manifest.Name + " (Unsigned)", manifest.Version, package.GetIcon());
#else
                    splash = _createSplashScreen(manifest.Name, manifest.Version, package.GetIcon());
#endif
                    splashWindow = (Window)splash;
                    metadata.UpdateLaunchStatus = s =>
                    {
                        if (splash != null && splashWindow != null && splashWindow.IsVisible)
                        {
                            splash.ShowText(s);
                        }
                    };
#if ENFORCE_PACKAGE_SECURITY
                    if (!isSigned)
                    {
                        splashWindow.Style = Application.Current.Resources["AlarmSplashScreenStyle"] as Style;
                    }
#endif
                    splashWindow.Show();
                }

                // Extract the application arguments from the command line
                Dictionary <string, object> args = null;
                if (cmdLine != null && _appArgumentParser != null)
                {
                    string appArgs, appUrl;
                    if (cmdLine.GetValue("args", out appArgs))
                    {
                        args = _appArgumentParser(appArgs);
                    }
                    else if (cmdLine.GetValue("url", out appUrl))
                    {
                        Uri uri;
                        if (Uri.TryCreate(appUrl, UriKind.Absolute, out uri))
                        {
                            if (!string.IsNullOrEmpty(uri.Query))
                            {
                                args = _appArgumentParser(uri.Query.Substring(1));
                            }
                        }
                    }
                }

                //Create and register application
                application = _createApplication(package, metadata, args);
                Register(application);

                // Launch the application
                var stopWatch = AutoStopwatch.TimeIt("Launching");

                application.Launched += delegate
                {
                    if (splashWindow != null)
                    {
                        splashWindow.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            if (splashWindow != null)
                            {
                                splashWindow.Close();
                            }
                        }));
                    }

                    this.RemoveSingleInstanceLaunchMarker(metadata.Id);

                    stopWatch.Dispose();
                };

                application.Launch();

                string protocolUri = null;
                if (cmdLine != null)
                {
                    cmdLine.GetValue("url", out protocolUri);
                    if (!string.IsNullOrEmpty(protocolUri))
                    {
                        application.OnProtocolInvoke(protocolUri);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Info("Error starting paragon application : {0}", ex);

                MessageBox.Show("Unable to start:\n\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                if (splashWindow != null)
                {
                    splashWindow.Close();
                }

                if (application != null)
                {
                    RemoveSingleInstanceLaunchMarker(metadata.Id);
                    application.Close();
                    application = null;
                }
            }
        }