using _29WxOrderWpf.BaseWindows; using CefSharp; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; using Work; using Work.BaseForms; namespace _29WxOrderWpf { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { public static int ProcessCount = 0; public static string[] args { get; set; } = new List().ToArray(); protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); if (e.Args.Length > 0) { if (e.Args.Length != 2 && e.Args.Any(a => a.Contains("--browser-subprocess-path"))) { //MessageBox.Show(string.Join(",", args)); Process.GetCurrentProcess().Kill(); return; } args = e.Args; } try { //设置应用程序处理异常方式:ThreadException处理 //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException; //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); //StartUp.Init(); new Initialization().Get(); #region 贴牌 //本地缓存文件 string IgPakFile = "locales/ig.pak";//贴牌客服端码 string OenVersionFile = "locales/igv.pak";//贴牌资源配置版本 string IaPakFile = "locales/ia.pak";//logo string IbPakFile = "locales/ib.pak";//ico string IcPakFile = "locales/ic.pak";//功能配置 //OenMechanism(IgPakFile, OenVersionFile, IaPakFile, IbPakFile, IcPakFile); #region 后台修改logo 贴牌用 //// 替换字典程序logo等 //Uri imageUri = new Uri("pack://application:,,,/Resources/WindowBoxes/close.hover.png"); // 新的图片路径 //BitmapImage newBitmapImage = new BitmapImage(imageUri); //// 获取资源字典 //ResourceDictionary dict = Application.Current.Resources; //// 添加或更新资源 //dict["main29WeixinIcon"] = newBitmapImage; #endregion #endregion } catch (Exception ex) { string str = GetExceptionMsg(ex, string.Empty); NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str); //BaseMsgForm.ShowError(str, "系统错误"); BaseMsgWindow.ShowError(str, "系统错误"); } #region 初始化cef if (!args.Any(a => a.Contains("--browser-subprocess-path"))) { CefSharpSettings.WcfEnabled = false; CefSharp.Wpf.CefSettings settings = new CefSharp.Wpf.CefSettings(); settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"; settings.CefCommandLineArgs.Add("disable-gpu", "1"); // 禁止显卡加速 settings.IgnoreCertificateErrors = true; settings.Locale = "zh-CN"; settings.LogSeverity = LogSeverity.Disable; var pName = Process.GetCurrentProcess().ProcessName; ProcessCount = Process.GetProcesses().Count(p => p.ProcessName == pName); //138版cef自己不会分缓存,会因占用导致报错,手动分缓存 settings.CachePath = Path.Combine(Environment.CurrentDirectory, "Cache\\Order" + ProcessCount); //settings.CachePath = null; //禁止缓存 //settings.CefCommandLineArgs.Add("enable-javascript", "1"); //settings.CefCommandLineArgs.Add("disable-features", "NetworkService,NetworkServiceInProcess"); settings.CefCommandLineArgs.Add("--disable-web-security", "1"); //settings.CefCommandLineArgs.Add("--disable-request-handling-for-testing", "1"); #if DEBUG settings.LogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cef_log_order.txt"); // 设置日志级别为详细 settings.LogSeverity = LogSeverity.Verbose; // 捕获JavaScript异常堆栈(最多32帧) settings.UncaughtExceptionStackSize = 32; #endif Cef.Initialize(settings); } #endregion } //static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) //{ // string str = GetExceptionMsg(e.Exception, e.ToString()); // NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str); // BaseMsgWindow.ShowError(str, "系统错误"); //} private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string str = GetExceptionMsg(e.Exception, e.ToString()); NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str); BaseMsgWindow.ShowError(str, "系统错误"); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str); BaseMsgWindow.ShowError(str, "系统错误"); } /// /// 生成自定义异常消息 /// /// 异常对象 /// 备用异常消息:当ex为null时有效 /// 异常字符串文本 static string GetExceptionMsg(Exception ex, string backStr) { StringBuilder sb = new StringBuilder(); sb.AppendLine("****************************异常文本****************************"); sb.AppendLine("【出现时间】:" + DateTime.Now.ToString()); if (ex != null) { sb.AppendLine("【异常类型】:" + ex.GetType().Name); sb.AppendLine("【异常信息】:" + ex.Message); sb.AppendLine("【堆栈调用】:" + ex.StackTrace); } else { sb.AppendLine("【未处理异常】:" + backStr); } sb.AppendLine("***************************************************************"); NaiBa.Util.Helper.LogHelper.WriteWithTime("Error:" + sb); BaseMsgWindow.ShowError(sb.ToString(), "系统错误"); return sb.ToString(); } /// /// Oen贴牌机制 /// /// 贴牌商客户端码文件 /// 贴牌配置版本号文件 /// ico本地缓存文件 /// logo本地缓存文件 /// 贴牌配置本地缓存文件 private static void OenMechanism(string IgPakFile, string OenVersionFile, string IaPakFile, string IbPakFile, string IcPakFile) { //todo: 贴牌相关处理逻辑 } } }