PccMainForm.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Microsoft.AspNetCore.Components.WebView;
  2. using Microsoft.AspNetCore.Components.WebView.WindowsForms;
  3. using PCC.AppScaffold;
  4. using PCC.Common.EventBus;
  5. using PCC.Common.Logging;
  6. namespace PCC.Gui
  7. {
  8. public partial class PccMainForm : Form
  9. {
  10. private IEventBus _eventBus;
  11. private bool _isShuttingDown = false;
  12. public PccMainForm()
  13. {
  14. InitializeComponent();
  15. }
  16. private async void PccMainForm_Shown(object sender, EventArgs e)
  17. {
  18. ApplicationInitializationStatusLabel.Text = "Building host...";
  19. Application.DoEvents();
  20. HostHolder.BuildHost();
  21. _eventBus = HostHolder.EventBus;
  22. void UseEvh(UiShownEvent obj)
  23. {
  24. ApplicationInitializationPanel.Hide();
  25. _eventBus.UnSubscript<UiShownEvent>(UseEvh);
  26. #if DEBUG
  27. MainBlazorWebView.WebView.CoreWebView2.OpenDevToolsWindow();
  28. #endif
  29. }
  30. _eventBus.Subscript((Action<UiShownEvent>)UseEvh);
  31. async void LocalMethod(LoggingEvent le)
  32. {
  33. if (ApplicationInitializationPanel.Visible == false) return;
  34. ApplicationInitializationStatusLabel.Text = $"{le.LogLevel} {le.Content}";
  35. Application.DoEvents();
  36. }
  37. HostHolder.EventBus.Subscript<LoggingEvent>(LocalMethod);
  38. ApplicationInitializationStatusLabel.Text = "Starting host...";
  39. Application.DoEvents();
  40. await HostHolder.StartAsync();
  41. void OnUrlLoading(object? o, UrlLoadingEventArgs args)
  42. {
  43. MainBlazorWebView.UrlLoading -= OnUrlLoading;
  44. ApplicationInitializationStatusLabel.Text = "Loading UI...";
  45. Application.DoEvents();
  46. }
  47. MainBlazorWebView.UrlLoading += OnUrlLoading;
  48. MainBlazorWebView.HostPage = "wwwroot\\index-mud-ise.html";
  49. MainBlazorWebView.Services = HostHolder.ServiceProvider;
  50. MainBlazorWebView.RootComponents.Add<MudIse.MainMudIse>("#app");
  51. }
  52. private void PccMainForm_FormClosing(object sender, FormClosingEventArgs e)
  53. {
  54. if (_isShuttingDown) return;
  55. _isShuttingDown = true;
  56. ApplicationInitializationPanel.Show();
  57. ApplicationInitializationTitleLabel.Text = "Application shutting down...";
  58. Application.DoEvents();
  59. HostHolder.StopAsync().Wait();
  60. ApplicationInitializationStatusLabel.Text = "Window will close after 3 second...";
  61. Application.DoEvents();
  62. Task.Delay(3000).Wait();
  63. }
  64. }
  65. }