App.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using CefSharp;
  2. using CefSharp.Wpf;
  3. using FISLib;
  4. using FISSDKDemoV2.Manager;
  5. using FISSDKDemoV2.Model.GC;
  6. using FISSDKDemoV2.Model.Log;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Threading;
  13. namespace FISSDKDemoV2
  14. {
  15. /// <summary>
  16. /// App.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. protected override void OnStartup(StartupEventArgs e)
  21. {
  22. CheckedOpenRepeatedly();
  23. var rootPath = "D:";
  24. if (!Directory.Exists(rootPath))
  25. {
  26. rootPath = Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory);
  27. }
  28. var fisLogPath = Path.Combine(rootPath, "FISLogFolder");
  29. Logger.RegisterEngine(new DefaultLogEngine(fisLogPath));
  30. GCLogger.RegisterEngine(new GCLogEngine(fisLogPath));
  31. GcAdapter.Instance.RegisterForFullGCNotification(99, 99);
  32. AppDomain.CurrentDomain.UnhandledException += OnDomainOnUnhandledException;
  33. Current.DispatcherUnhandledException += OnDispatcherUnhandledException;
  34. InitCefSharp();
  35. base.OnStartup(e);
  36. }
  37. private void InitCefSharp()
  38. {
  39. try
  40. {
  41. var settings = new CefSettings
  42. {
  43. Locale = "zh-CN",
  44. LogSeverity = LogSeverity.Disable,
  45. IgnoreCertificateErrors = true,
  46. BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CefSharp", "x64", "CefSharp.BrowserSubprocess.exe"),
  47. };
  48. //允许媒体自动播放,而无需用户交互。
  49. settings.CefCommandLineArgs.Add("autoplay-policy", "no-user-gesture-required");
  50. //启用打印预览功能
  51. settings.CefCommandLineArgs.Add("--enable-print-preview", "1");
  52. //允许使用媒体流(例如麦克风和摄像头)。
  53. settings.CefCommandLineArgs.Add("enable-media-stream", "1");
  54. //允许屏幕捕捉,这通常用于屏幕共享或录制。
  55. settings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing", "1");
  56. //允许语音输入
  57. settings.CefCommandLineArgs.Add("enable-speech-input", "1");
  58. //禁用Web安全策略,允许跨源请求,但这可能会带来安全风险
  59. settings.CefCommandLineArgs.Add("--disable-web-security", "1");
  60. //允许HTTP屏幕捕获
  61. settings.CefCommandLineArgs.Add("allow-http-screen-capture", "1");
  62. //自动选择桌面捕捉源为整个屏幕
  63. settings.CefCommandLineArgs.Add("--auto-select-desktop-capture-source", "Entire screen");
  64. //使用假的UI来请求媒体流(例如摄像头和麦克风),而不显示真实的权限请求对话框
  65. settings.CefCommandLineArgs.Add("--use-fake-ui-for-media-stream", "1");
  66. //启用专有编解码器,这可以支持更多的视频格式
  67. settings.CefCommandLineArgs.Add("proprietary_codecs", "1");
  68. //启用剪贴板访问
  69. settings.CefCommandLineArgs.Add("--enable-clipboard", "1");
  70. //使用Chrome版本的FFmpeg,这可以支持更多的媒体格式
  71. settings.CefCommandLineArgs.Add("ffmpeg_branding", "Chrome");
  72. // 允许CEF访问剪贴板
  73. settings.CefCommandLineArgs.Add("allow-cef-access-to-clipboard", "1");
  74. //允许从文件访问文件,通常用于本地HTML页面可以读取其他本地文件。
  75. settings.CefCommandLineArgs.Add("allow-file-access-from-files", "1");
  76. settings.CefCommandLineArgs.Add("unsafely-treat-insecure-origin-as-secure", "http://192.168.6.103:8081,http://192.168.6.62:8081,http://192.168.6.71:8081,http://192.168.6.117:8081");
  77. //CefSharpSettings.LegacyJavascriptBindingEnabled = true;
  78. CefSharpSettings.ShutdownOnExit = false;
  79. #if DEBUG
  80. settings.RemoteDebuggingPort = 8088;
  81. #endif
  82. Cef.Initialize(settings);
  83. }
  84. catch (Exception ex)
  85. {
  86. Logger.WriteLineError($"CefInit Error:{ex}");
  87. PageManager.Instance.ShowMessageBoxInMainWindow(FISDeviceLogCategory.Error, $"CefInit Error:{ex}");
  88. }
  89. }
  90. private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  91. {
  92. Logger.WriteLineError($"OnDispatcherUnhandledException:{e.Exception}");
  93. e.Handled = true;
  94. PageManager.Instance.ShowMessageBoxInMainWindow(FISDeviceLogCategory.Error, $"OnDispatcherUnhandledException{e.Exception}");
  95. }
  96. private void OnDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
  97. {
  98. Logger.WriteLineError($"OnDomainOnUnhandledException:{e.ExceptionObject}");
  99. PageManager.Instance.ShowMessageBoxInMainWindow(FISDeviceLogCategory.Error, $"OnDomainOnUnhandledException:{e.ExceptionObject}");
  100. }
  101. /// <summary>
  102. /// 判断是否重复打开
  103. /// </summary>
  104. private void CheckedOpenRepeatedly()
  105. {
  106. var thisProcess = Process.GetCurrentProcess();
  107. if (Process.GetProcessesByName(thisProcess.ProcessName).Where(x => x.ProcessName == thisProcess.ProcessName).Count() > 1)
  108. {
  109. PageManager.Instance.ShowMessageBoxInMainWindow(FISDeviceLogCategory.Warn, $"已有相同程序正在运行中,此程序即将退出。");
  110. thisProcess.Kill();
  111. }
  112. }
  113. }
  114. }