瀏覽代碼

Improved BaseApplication

LukePulverenti Luke Pulverenti luke pulverenti 13 年之前
父節點
當前提交
aa64577b21
共有 2 個文件被更改,包括 72 次插入28 次删除
  1. 61 5
      MediaBrowser.Common/UI/BaseApplication.cs
  2. 11 23
      MediaBrowser.ServerApplication/App.xaml.cs

+ 61 - 5
MediaBrowser.Common/UI/BaseApplication.cs

@@ -1,21 +1,34 @@
-using System;
-using System.Windows;
-using MediaBrowser.Common.Kernel;
+using MediaBrowser.Common.Kernel;
 using MediaBrowser.Common.Logging;
 using MediaBrowser.Common.Logging;
 using MediaBrowser.Model.Progress;
 using MediaBrowser.Model.Progress;
+using Microsoft.Shell;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Windows;
 
 
 namespace MediaBrowser.Common.UI
 namespace MediaBrowser.Common.UI
 {
 {
     /// <summary>
     /// <summary>
     /// Serves as a base Application class for both the UI and Server apps.
     /// Serves as a base Application class for both the UI and Server apps.
     /// </summary>
     /// </summary>
-    public abstract class BaseApplication : Application
+    public abstract class BaseApplication : Application, INotifyPropertyChanged, ISingleInstanceApp
     {
     {
         private IKernel Kernel { get; set; }
         private IKernel Kernel { get; set; }
 
 
         protected abstract IKernel InstantiateKernel();
         protected abstract IKernel InstantiateKernel();
         protected abstract Window InstantiateMainWindow();
         protected abstract Window InstantiateMainWindow();
 
 
+        public event PropertyChangedEventHandler PropertyChanged;
+
+        public void OnPropertyChanged(String info)
+        {
+            if (PropertyChanged != null)
+            {
+                PropertyChanged(this, new PropertyChangedEventArgs(info));
+            }
+        }
+
         protected override void OnStartup(StartupEventArgs e)
         protected override void OnStartup(StartupEventArgs e)
         {
         {
             // Without this the app will shutdown after the splash screen closes
             // Without this the app will shutdown after the splash screen closes
@@ -43,7 +56,10 @@ namespace MediaBrowser.Common.UI
                 splash.Close();
                 splash.Close();
 
 
                 ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
                 ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
-                InstantiateMainWindow().ShowDialog();
+
+                OnKernelLoaded();
+
+                InstantiateMainWindow().Show();
             }
             }
             catch (Exception ex)
             catch (Exception ex)
             {
             {
@@ -60,11 +76,51 @@ namespace MediaBrowser.Common.UI
             }
             }
         }
         }
 
 
+        protected virtual void OnKernelLoaded()
+        {
+
+        }
+
         protected override void OnExit(ExitEventArgs e)
         protected override void OnExit(ExitEventArgs e)
         {
         {
             base.OnExit(e);
             base.OnExit(e);
 
 
             Kernel.Dispose();
             Kernel.Dispose();
         }
         }
+
+        public bool SignalExternalCommandLineArgs(IList<string> args)
+        {
+            OnSecondInstanceLaunched(args);
+
+            return true;
+        }
+
+        protected virtual void OnSecondInstanceLaunched(IList<string> args)
+        {
+            if (this.MainWindow.WindowState == WindowState.Minimized)
+            {
+                this.MainWindow.WindowState = WindowState.Maximized;
+            }            
+        }
+
+        public static void RunApplication<TApplicationType>(string uniqueKey)
+            where TApplicationType : BaseApplication, IApplication, new()
+        {
+            if (SingleInstance<TApplicationType>.InitializeAsFirstInstance(uniqueKey))
+            {
+                var application = new TApplicationType();
+                application.InitializeComponent();
+
+                application.Run();
+
+                // Allow single instance code to perform cleanup operations
+                SingleInstance<TApplicationType>.Cleanup();
+            }
+        }
+    }
+
+    public interface IApplication
+    {
+        void InitializeComponent();
     }
     }
 }
 }

+ 11 - 23
MediaBrowser.ServerApplication/App.xaml.cs

@@ -1,44 +1,32 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Windows;
-using MediaBrowser.Common.Kernel;
+using MediaBrowser.Common.Kernel;
 using MediaBrowser.Common.UI;
 using MediaBrowser.Common.UI;
 using MediaBrowser.Controller;
 using MediaBrowser.Controller;
 using Microsoft.Shell;
 using Microsoft.Shell;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Windows;
 
 
 namespace MediaBrowser.ServerApplication
 namespace MediaBrowser.ServerApplication
 {
 {
     /// <summary>
     /// <summary>
     /// Interaction logic for App.xaml
     /// Interaction logic for App.xaml
     /// </summary>
     /// </summary>
-    public partial class App : BaseApplication, ISingleInstanceApp
+    public partial class App : BaseApplication, IApplication
     {
     {
-        private const string Unique = "MediaBrowser3";
-
         [STAThread]
         [STAThread]
         public static void Main()
         public static void Main()
         {
         {
-            if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
-            {
-                var application = new App();
-                application.InitializeComponent();
-
-                application.Run();
-
-                // Allow single instance code to perform cleanup operations
-                SingleInstance<App>.Cleanup();
-            }
+            RunApplication<App>("MediaBrowserServer");
         }
         }
 
 
-        #region ISingleInstanceApp Members
-        public bool SignalExternalCommandLineArgs(IList<string> args)
+        protected override void OnSecondInstanceLaunched(IList<string> args)
         {
         {
-            OpenDashboard();
+            base.OnSecondInstanceLaunched(args);
 
 
-            return true;
+            OpenDashboard();
+            InitializeComponent();
         }
         }
-        #endregion
 
 
         public static void OpenDashboard()
         public static void OpenDashboard()
         {
         {