alielie
2025-03-23 40014e6e41f1ec017a07b7a620b3c700807d0519
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using HMI.Core.Setting;
using HMI.Core.Util;
using HMI.UI.Util;
using log4net;
using Panuon.WPF.UI;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;
using WPFProject.Utils;
 
namespace WPFProject
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private ILog log;
        public App()
        {
            log = LogManager.GetLogger("APP");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }
 
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            log.Error("UnhandledException.", ex);
            MsgBoxHelper.ErrorMessage(Current.MainWindow, this.lang("App.AppUnhandledException",ex.Message),
                    this.lang("App.Error"), MessageBoxButton.OK,
                    MessageBoxIcon.Error, DefaultButton.YesOK, 5);
        }
 
        private static Mutex mutex;
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            mutex = new Mutex(true, "TestPlatform_OnlyRun_CRNS");
            if (mutex.WaitOne(0, false))
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
 
                LangUtil.Load();
                SystemSetting.SystemSettingChanged += SystemSetting_SystemSettingChanged;
                ToastHelper.Register(new MainToastHandler());
                //LoadingWindow window = new LoadingWindow();
                Login window = new Login();
                window.Show();
            }
            else
            {
                MessageBox.Show("程序已经在运行!", "提示");
                this.Shutdown();
            }
        }
 
        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            // 获取请求加载的程序集名称
            string assemblyName = new AssemblyName(args.Name).Name;
 
            string[] dirs = new string[] { "3rds", "Libs" };
 
            foreach (string dir in dirs)
            {
                string dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dir, $"{assemblyName}.dll");
                if (File.Exists(dllPath))
                {
                    // 如果依赖 DLL 存在,则加载并返回它
                    return Assembly.LoadFrom(dllPath);
                }
            }
            // 否则返回 null
            return null;
        }
 
        private void SystemSetting_SystemSettingChanged(object sender, SystemSetting e)
        {
            LangUtil.LoadLang();
        }
    }
}