Exemplo n.º 1
0
        private async void ApplicationBase_Startup(object sender, StartupEventArgs e)
        {
            this.isInitialized = true;

            if (this.UrlProtocolNames != null && this.UrlProtocolNames.Length > 0)
            {
                for (int i = 0; i < this.UrlProtocolNames.Length; i++)
                {
                    if (UrlProtocol.RequiresRegistration(this.UrlProtocolNames[i]))
                    {
                        this.OnUrlProtocolRegistration(this.UrlProtocolNames[i]);
                    }
                }

                if (e.Args.Length > 0 && e.Args[0] == "registerUriScheme")
                {
                    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    this.Shutdown(0);
                    return;
                }
            }

            // If an application is being run by VS then it will have the .vshost suffix to its proc
            // name. We have to also check them
            var proc        = Process.GetCurrentProcess();
            var processName = proc.ProcessName.Replace(".vshost", "");
            var processes   = Process.GetProcesses().Where(x => (x.ProcessName == processName || x.ProcessName == proc.ProcessName || x.ProcessName == proc.ProcessName + ".vshost") && x.Id != proc.Id).ToArray();

            // Special case ... If we recieve a call from an uri protocol we will be passing this to
            // all instances of the application
            if (this.UrlProtocolNames != null &&
                this.UrlProtocolNames.Length > 0 &&
                e.Args.Length > 0 &&
                processes.Length > 0 &&
                this.IsUrlProtocol(e.Args))
            {
                foreach (var process in processes.Where(x => x.Id != proc.Id))
                {
                    Win32Api.SendMessage(process.MainWindowHandle, $"{e.Args.Join("\n")}");
                }

                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                this.Shutdown();
            }

            if (this.IsSingleInstance && processes.Length > 0)
            {
                var hwnd = processes[0].MainWindowHandle;

                if (hwnd != IntPtr.Zero)
                {
                    Win32Api.SendMessage(hwnd, $"{e.Args.Join("\n")}");
                    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    this.Shutdown();
                }
            }
            else
            {
                if (this.IsSinglePage)
                {
                    WindowType windowType = null;
                    var        window     = Common.CreateWindow(ref windowType);

                    window.ContentTemplateSelector = new CauldronTemplateSelector();

                    window.MinHeight             = 353;
                    window.MinWidth              = 502;
                    window.ShowInTaskbar         = true;
                    window.Topmost               = false;
                    window.WindowStartupLocation = WindowStartupLocation.Manual;
                    window.WindowState           = WindowState.Normal;
                    window.Icon = await Win32Api.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location).ToBitmapImageAsync();

                    window.Title = ApplicationInfo.ApplicationName;

                    PersistentWindowInformation.Load(window, this.GetType());

                    window.SizeChanged += (s, e1) =>
                    {
                        if (window.WindowState == WindowState.Normal)
                        {
                            PersistentWindowProperties.SetHeight(window, e1.NewSize.Height);
                            PersistentWindowProperties.SetWidth(window, e1.NewSize.Width);
                        }
                    };
                    window.Closing += (s, e1) => PersistentWindowInformation.Save(window, this.GetType());

                    window.Show();

                    window.Content = this;
                    window.Activate();
                    await this.OnPreload();

                    var rootFrame = new NavigationFrame();
                    rootFrame.DataContext = this;
                    window.Content        = rootFrame;
                    window.InputBindings.Add(new KeyBinding(new RelayCommand(async() => { await rootFrame.GoBack(); }, () => rootFrame.CanGoBack), Key.Back, ModifierKeys.None));
                }
                else if (this.GetType().GetCustomAttribute <ViewAttribute>() != null || Application.Current.Resources.Contains($"View_{this.GetType().Name}"))
                {
                    this.Navigator.As <Navigator>()?.NavigateInternal <ApplicationBase>(this, null, null);
                    await this.OnPreload();

                    Application.Current.MainWindow.Activate();
                }

                await this.OnStartup(new LaunchActivatedEventArgs(e.Args));

                this.Navigator.TryClose(this);

                if (Application.Current.MainWindow != null)
                {
                    HwndSource.FromHwnd(Application.Current.MainWindow.GetWindowHandle())?.AddHook(new HwndSourceHook(HandleMessages));
                    Application.Current.MainWindow.Activate();
                }
            }
        }
Exemplo n.º 2
0
        private async void ApplicationBase_Startup(object sender, StartupEventArgs e)
        {
            if (this.isInitialized)
            {
                return;
            }

            this.isInitialized = true;

            if (e.Args != null && e.Args.Length > 0 && e.Args[0] == "registerUriScheme")
            {
                try
                {
                    RegisterUrlProtocols();
                }
                catch
                {
                    // don't show anything... This will only annoy users
                }

                this.Shutdown();
                return;
            }

            ParamPassing.Configure(e.Args, x =>
            {
                x.IsSingleInstance     = this.IsSingleInstance;
                x.BringToFront         = this.ShouldBringToFront;
                x.RandomSelectInstance = true;
                x.DataSeparator        = '\n';

                x.ParameterPassedCallback = new Action <string[]>(args =>
                {
                    if (args != null && args.Length > 0 && args[0].IndexOf(':') > 0 &&
                        ApplicationUrlProtocols.Contains(args[0].Split(':').First(),
                                                         new DynamicEqualityComparer <string>((a, b) => string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase))))
                    {
                        try
                        {
                            this.OnActivationProtocol(new Uri(args[0]));
                        }
                        catch
                        {
                            // if the application recieves malformed or bad urls... We should just ignore it here
                        }
                    }
                    else
                    {
                        this.OnActivated(args);
                    }
                });

                x.ExitDelegate = () =>
                {
                    this.Shutdown();
                    Environment.Exit(0);
                };
            });

            if (this.IsSingleInstance && ParamPassing.AreOtherInstanceActive)
            {
                if (ParamPassing.BringToFront())
                {
                    this.Shutdown();
                }
            }
            else
            {
                if (this.IsSinglePage)
                {
                    WindowType windowType = null;
                    var        window     = Common.CreateWindow(ref windowType);

                    window.ContentTemplateSelector = new CauldronTemplateSelector();

                    window.MinHeight             = 353;
                    window.MinWidth              = 502;
                    window.ShowInTaskbar         = true;
                    window.Topmost               = false;
                    window.WindowStartupLocation = WindowStartupLocation.Manual;
                    window.WindowState           = WindowState.Normal;
                    window.Icon = await UnsafeNative.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location).ToBitmapImageAsync();

                    window.Title = ApplicationInfo.ApplicationName;

                    PersistentWindowInformation.Load(window, this.GetType());

                    window.SizeChanged += (s, e1) =>
                    {
                        if (window.WindowState == WindowState.Normal)
                        {
                            PersistentWindowProperties.SetHeight(window, e1.NewSize.Height);
                            PersistentWindowProperties.SetWidth(window, e1.NewSize.Width);
                        }
                    };
                    window.Closing += (s, e1) => PersistentWindowInformation.Save(window, this.GetType());

                    window.Show();

                    window.Content = this;
                    window.Activate();
                    await this.OnPreload();

                    var rootFrame = new NavigationFrame();
                    rootFrame.DataContext = this;
                    window.Content        = rootFrame;
                    window.InputBindings.Add(new KeyBinding(new RelayCommand(async() => { await rootFrame.GoBack(); }, () => rootFrame.CanGoBack), Key.Back, ModifierKeys.None));
                }
                else if (this.GetType().GetCustomAttribute <ViewAttribute>() != null || Application.Current.Resources.Contains($"View_{this.GetType().Name}"))
                {
                    var oldShutdownMode = Application.Current.ShutdownMode;
                    Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    this.Navigator.As <Navigator>()?.NavigateInternal <ApplicationBase>(this, null, null);
                    await this.OnPreload();

                    if (Application.Current.MainWindow == null)
                    {
                        Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                        Application.Current.Shutdown();
                        return;
                    }

                    Application.Current.MainWindow.Activate();
                    this.Navigator.TryClose(this);
                    Application.Current.ShutdownMode = oldShutdownMode;
                }

                Application.Current.MainWindow = null;
                await this.OnStartup(new LaunchActivatedEventArgs(e.Args));

                if (Application.Current.MainWindow != null)
                {
                    Application.Current.MainWindow.AddHookParameterPassing();
                    Application.Current.MainWindow.Activate();
                }
            }
        }
Exemplo n.º 3
0
        private async void ApplicationBase_Startup(object sender, StartupEventArgs e)
        {
            this.isInitialized = true;

            ParamPassing.Configure(e.Args, x =>
            {
                x.IsSingleInstance     = this.IsSingleInstance;
                x.BringToFront         = this.ShouldBringToFront;
                x.RandomSelectInstance = true;
                x.DataSeparator        = '\n';

                x.ParameterPassedCallback = new Action <string[]>(args => this.OnActivated(args));

                x.ExitDelegate = () =>
                {
                    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    this.Shutdown();
                };
            });

            if (this.IsSingleInstance && ParamPassing.AreOtherInstanceActive)
            {
                if (ParamPassing.BringToFront())
                {
                    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    this.Shutdown();
                }
            }
            else
            {
                if (this.IsSinglePage)
                {
                    WindowType windowType = null;
                    var        window     = Common.CreateWindow(ref windowType);

                    window.ContentTemplateSelector = new CauldronTemplateSelector();

                    window.MinHeight             = 353;
                    window.MinWidth              = 502;
                    window.ShowInTaskbar         = true;
                    window.Topmost               = false;
                    window.WindowStartupLocation = WindowStartupLocation.Manual;
                    window.WindowState           = WindowState.Normal;
                    window.Icon = await UnsafeNative.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location).ToBitmapImageAsync();

                    window.Title = ApplicationInfo.ApplicationName;

                    PersistentWindowInformation.Load(window, this.GetType());

                    window.SizeChanged += (s, e1) =>
                    {
                        if (window.WindowState == WindowState.Normal)
                        {
                            PersistentWindowProperties.SetHeight(window, e1.NewSize.Height);
                            PersistentWindowProperties.SetWidth(window, e1.NewSize.Width);
                        }
                    };
                    window.Closing += (s, e1) => PersistentWindowInformation.Save(window, this.GetType());

                    window.Show();

                    window.Content = this;
                    window.Activate();
                    await this.OnPreload();

                    var rootFrame = new NavigationFrame();
                    rootFrame.DataContext = this;
                    window.Content        = rootFrame;
                    window.InputBindings.Add(new KeyBinding(new RelayCommand(async() => { await rootFrame.GoBack(); }, () => rootFrame.CanGoBack), Key.Back, ModifierKeys.None));
                }
                else if (this.GetType().GetCustomAttribute <ViewAttribute>() != null || Application.Current.Resources.Contains($"View_{this.GetType().Name}"))
                {
                    this.Navigator.As <Navigator>()?.NavigateInternal <ApplicationBase>(this, null, null);
                    await this.OnPreload();

                    Application.Current.MainWindow.Activate();
                }

                await this.OnStartup(new LaunchActivatedEventArgs(e.Args));

                this.Navigator.TryClose(this);

                if (Application.Current.MainWindow != null)
                {
                    Application.Current.MainWindow.AddHookParameterPassing();
                    Application.Current.MainWindow.Activate();
                }
            }
        }