using DryIoc;
using PLCSharp.Core.Prism;
using System.Text.Json;
using System.Text.Json.Nodes;
using PLCSharp.Models;
using PLCSharp.VVMs.Authority;
using PLCSharp.VVMs.MainWindow;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
namespace PLCSharp
{
///
/// 应用程序入口,负责初始化 Prism 框架和注册组件
///
public partial class App
{
private Mutex mutex;
//第1执行
///
/// 注册应用程序所需的类型和服务
///
/// Prism 容器注册器
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
mutex = new Mutex(true, "PLCSharp", out bool ret);
if (!ret)
{
ForegroundWindow();
Environment.Exit(0);
}
LoadConfig();
//数据库
_ = containerRegistry.RegisterSingleton();
_ = Container.Resolve();
RegisterEvents(); // 捕获全局异常
//Module中的初始化晚于这里,所以不能在Module中注册全局使用的实例,比如NavigateModel
//注册Model特性的类,单例
var models = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetCustomAttribute() is not null
select t;
foreach (var model in models)
{
_ = containerRegistry.RegisterSingleton(model);
}
//注册导航特性的类到导航
var views = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetCustomAttribute() is not null
select t;
foreach (var view in views)
{
containerRegistry.RegisterForNavigation(view, view.Name);
}
//注册弹出窗口特性的类,到导航
var dialogs = from t in Assembly.GetExecutingAssembly().GetTypes() //
where t.GetCustomAttribute() is not null
select t;
foreach (var dialog in dialogs)
{
containerRegistry.RegisterForNavigation(dialog, dialog.Name);
}
//反射要加入菜单的弹出窗口特性的类,注册到导航
var dialogsMenu = from t in Assembly.GetExecutingAssembly().GetTypes() //
where t.GetCustomAttribute() is not null
select t;
foreach (var dialog in dialogsMenu)
{
containerRegistry.RegisterForNavigation(dialog, dialog.Name);
}
}
//第2执行
///
/// 配置模块目录
///
/// 模块目录
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//_ = moduleCatalog.AddModule();
}
//第3执行
///
/// 创建Shell
///
/// 返回结果
protected override Window CreateShell()
{
//反射需要导航的页面
var navigate = Container.Resolve();
var views = from t in Assembly.GetExecutingAssembly().GetTypes() //
where t.GetCustomAttribute() is not null
select t;
foreach (var view in views)
{
var page = view.GetCustomAttribute();
var navigteItem = Container.Resolve();
navigteItem.ViewName = page.ViewName;
navigteItem.IconKind = page.IconKind;
navigteItem.DisplayName = page.DisplayName;
navigteItem.UserLevel = page.UserLevel;
navigteItem.Index = page.Index;
navigate.NavigateList.Add(navigteItem);
}
//反射需要弹出的窗口
var dialogs = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetCustomAttribute() is not null
select t;
foreach (var dialog in dialogs)
{
var page = dialog.GetCustomAttribute();
var navigteItem = Container.Resolve();
navigteItem.ViewName = page.ViewName;
navigteItem.IconKind = page.IconKind;
navigteItem.DisplayName = page.DisplayName;
navigteItem.UserLevel = page.UserLevel;
navigteItem.Index = page.Index;
navigate.DialogList.Add(navigteItem);
}
var window = Container.Resolve();
var user = Container.Resolve();
#if DEBUG
user.CurrentUser = user.UserList.Where(u => u.Name == "admin").FirstOrDefault();
#else
user.CurrentUser = user.UserList.Where(u => u.Name == "guest").FirstOrDefault();
#endif
//设置默认页面
navigate.DefaultView = "Homepage";
_ = navigate.NavigateToAsync(navigate.DefaultView);
//启动主窗体
return window;
}
///
/// 自动寻找ViewModel并绑定到View,要求ViewModel命名为View的全名加上ViewModel,比如MainWindow的ViewModel命名为MainWindowViewModel
///
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
{
var viewName = viewType.FullName;
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";
return Type.GetType(viewModelName);
});
}
///
/// 加载环境变量
///
private static void LoadConfig()
{
if (!Directory.Exists("./Config/"))
{
Directory.CreateDirectory("./Config/");
}
if (File.Exists("./Config/EnvironmentVariable.json"))
{
var json = File.ReadAllText("./Config/EnvironmentVariable.json");
var config = JsonNode.Parse(json);
try
{
if (config?["EnvironmentVariables"] is JsonArray envVars)
{
foreach (var item in envVars)
{
string? name = item?["Name"]?.GetValue();
string? value = item?["Value"]?.GetValue();
if (name != null)
Environment.SetEnvironmentVariable(name, value ?? "");
}
}
}
catch (Exception)
{
}
}
}
[LibraryImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
private static partial bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
///
/// Foreground窗口
///
public static void ForegroundWindow()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id != currentProcess.Id)
{
// 将现有实例的窗口切换到前台
IntPtr hWnd = process.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
ShowWindowAsync(hWnd, 9);
SetForegroundWindow(hWnd);
}
break;
}
}
}
private void RegisterEvents()
{
// 后台线程异常的捕获方法
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
RecordLog(args.Exception.ToString());
args.SetObserved();
};
// 全局异常的捕获方法
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
RecordLog(args.ExceptionObject.ToString());
};
}
///
/// 记录日志
///
/// 显示Mag
public void RecordLog(string showMag)
{
lock (this)
{
var path = Environment.CurrentDirectory + @"\DebugLog\"; //文件夹路径
var paths = path + DateTime.Now.ToString("yyyyMMdd") + ".txt"; //文件路径 以年月日为名称
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//打开或创建文件
var fs = File.Open(paths, FileMode.Append);
//获得字节数组
var data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + "\r\n" + showMag + "\r\n");
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
}
}
}