BaseApplication.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using MediaBrowser.Common.Kernel;
  8. using MediaBrowser.Common.Logging;
  9. using MediaBrowser.Model.Progress;
  10. using System.Reflection;
  11. namespace MediaBrowser.Common.UI
  12. {
  13. /// <summary>
  14. /// Serves as a base Application class for both the UI and Server apps.
  15. /// </summary>
  16. public abstract class BaseApplication : Application
  17. {
  18. private IKernel Kernel { get; set; }
  19. protected abstract IKernel InstantiateKernel();
  20. protected abstract Window InstantiateMainWindow();
  21. protected override void OnStartup(StartupEventArgs e)
  22. {
  23. // Without this the app will shutdown after the splash screen closes
  24. this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  25. LoadKernel();
  26. }
  27. private async void LoadKernel()
  28. {
  29. Kernel = InstantiateKernel();
  30. Progress<TaskProgress> progress = new Progress<TaskProgress>();
  31. Splash splash = new Splash(progress);
  32. splash.Show();
  33. try
  34. {
  35. DateTime now = DateTime.Now;
  36. await Kernel.Init(progress);
  37. Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.Now - now).TotalSeconds);
  38. splash.Close();
  39. this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  40. InstantiateMainWindow().ShowDialog();
  41. }
  42. catch (Exception ex)
  43. {
  44. if (Logger.LoggerInstance != null)
  45. {
  46. Logger.LogException(ex);
  47. }
  48. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  49. splash.Close();
  50. // Shutdown the app with an error code
  51. Shutdown(1);
  52. }
  53. }
  54. protected override void OnExit(ExitEventArgs e)
  55. {
  56. base.OnExit(e);
  57. Kernel.Dispose();
  58. }
  59. public BitmapImage GetLogoImage()
  60. {
  61. BitmapImage bitmap = new BitmapImage();
  62. bitmap.CacheOption = BitmapCacheOption.Default;
  63. bitmap.BeginInit();
  64. bitmap.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Common.Resources.mblogo.png");
  65. bitmap.EndInit();
  66. return bitmap;
  67. }
  68. }
  69. }