HuNan_NOSqlSugar/DM_Weight/App.xaml.cs

368 lines
16 KiB
C#
Raw Permalink Normal View History

2024-07-06 10:01:30 +08:00
using DM_Weight.Finger;
using DM_Weight.Port;
using DM_Weight.util.TabTip;
using DM_Weight.util;
using DM_Weight.ViewModels;
using DM_Weight.Views.Dialog;
using DM_Weight.Views;
using log4net.Config;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Prism.Unity;
using log4net;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Timers;
using SqlSugar;
using Microsoft.Extensions.DependencyInjection;
using DM_Weight.Services;
using DM_Weight.Models;
using System.Diagnostics;
using System.Runtime.InteropServices;
//using DM_Dal;
//using DM_Dal.Models;
namespace DM_Weight
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
#region
private static UserList _currentFaUserList = new UserList();
public static UserList CurrentFaUserList
{
get => _currentFaUserList;
set => _currentFaUserList = value;
}
#endregion
#region
private static UserList _currentShenUserList = new UserList();
public static UserList CurrentShenUserList
{
get => _currentShenUserList;
set => _currentShenUserList = value;
}
#endregion
private readonly ILog logger = LogManager.GetLogger(typeof(App));
public App()
{
TabTipAutomation.IgnoreHardwareKeyboard = HardwareKeyboardIgnoreOptions.IgnoreAll;
TabTipAutomation.BindTo<TextBox>();
TabTipAutomation.BindTo<PasswordBox>();
}
protected override Window CreateShell()
{
//UI线程未捕获异常处理事件
this.DispatcherUnhandledException += OnDispatcherUnhandledException;
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
//多线程异常
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
return Container.Resolve<MainWindow>();
}
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
logger.Error($"发生错误:{e.Exception.Message}");
e.Handled = true;
}
void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
foreach (Exception item in e.Exception.InnerExceptions)
{
logger.Error($"异常类型:{item.StackTrace};{item.GetType()}{Environment.NewLine}来自:{item.Source}{Environment.NewLine}异常内容:{item.Message}");
if (item.GetType() == typeof(SqlSugar.SqlSugarException))
{
//UserList userList = new SqlHelperService().CheckUserByFingerPrinter(1);
}
}
//将异常标识为已经观察到
e.SetObserved();
}
void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
logger.Error($"Unhandled exception.{e.ToString()}");
}
protected override void InitializeShell(Window shell)
{
base.InitializeShell(shell);
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// 注入日志
XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
//containerRegistry.RegisterInstance<ILog>(LogManager.GetLogger(""));
// 串口工具
containerRegistry.RegisterSingleton<PortUtil>();
// 指纹机工具
containerRegistry.RegisterSingleton<FingerprintUtil>();
// 组态屏工具
containerRegistry.RegisterSingleton<ScreenUtil>();
containerRegistry.RegisterSingleton<SqlSugarHelper>();
containerRegistry.Register<IDialogService, MaterialDialogService>();
// 主窗口
containerRegistry.Register<MainWindow>();
containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>();
// 分页
//containerRegistry.Register<Pagination>();
//containerRegistry.Register<PaginationViewModel>();
// 登录页面
containerRegistry.RegisterForNavigation<LoginWindow, LoginWindowViewModel>();
// 布局页面
containerRegistry.RegisterForNavigation<HomeWindow, HomeWindowViewModel>();
// 录入指纹模态框
containerRegistry.RegisterDialog<FingerprintDialog>();
containerRegistry.RegisterForNavigation<FingerprintDialog, FingerprintDialogViewModel>();
#region
// 处方取药页面
containerRegistry.RegisterForNavigation<OrderTakeDrugWindow, OrderTakeDrugWindowViewModel>();
// 处方取药模态框
containerRegistry.RegisterDialog<OrderTakeDialog>();
containerRegistry.RegisterForNavigation<OrderTakeDialog, OrderTakeDialogViewModel>();
// 调拨取药页面
containerRegistry.RegisterForNavigation<InvoiceOutWindow, InvoiceOutWindowViewModel>();
// 调拨取药模态框
containerRegistry.RegisterDialog<InvoiceTakeDialog>();
containerRegistry.RegisterForNavigation<InvoiceTakeDialog, InvoiceTakeDialogViewModel>();
// 抽屉取药页面
containerRegistry.RegisterForNavigation<DrawerTakeDrugWindow, DrawerTakeDrugWindowViewModel>();
// 自选取药模态框
containerRegistry.RegisterDialog<SelfTakeDialog>();
containerRegistry.RegisterForNavigation<SelfTakeDialog, SelfTakeDialogViewModel>();
// 自选取药页面
containerRegistry.RegisterForNavigation<SelfTakeDrugWindow, SelfTakeDrugWindowViewModel>();
#endregion
#region
// 自选加药页面
containerRegistry.RegisterForNavigation<SelfAddWindow, SelfAddWindowViewModel>();
// 调拨加药页面
//containerRegistry.RegisterForNavigation<InvoiceInWindow, InvoiceInWindowViewModel>();
// 调拨加药页面
containerRegistry.RegisterForNavigation<InvoiceInNewWindow, InvoiceInNewWindowViewModel>();
// 调拨取药模态框
//containerRegistry.RegisterDialog<InvoiceAddDialog>();
//containerRegistry.RegisterForNavigation<InvoiceAddDialog, InvoiceAddDialogViewModel>();
// 抽屉加药页面
containerRegistry.RegisterForNavigation<DrawerAddDrugWindow, DrawerAddDrugWindowViewModel>();
// 自选加药模态框
containerRegistry.RegisterDialog<SelfAddDialog>();
containerRegistry.RegisterForNavigation<SelfAddDialog, SelfAddDialogViewModel>();
//多批次抽屉加药
containerRegistry.RegisterForNavigation<AddDrugControl, AddDrugControlViewModel>();
#endregion
#region
// 还药页面
containerRegistry.RegisterForNavigation<ReturnDrugWindow, ReturnDrugWindowViewModel>();
// 按记录归还药品模态框
containerRegistry.RegisterDialog<ReturnDrugDialog>();
containerRegistry.RegisterForNavigation<ReturnDrugDialog, ReturnDrugDialogViewModel>();
// 还药页面2
containerRegistry.RegisterForNavigation<ReturnDrugWindow2, ReturnDrugWindow2ViewModel>();
// 按处方归还药品模态框
containerRegistry.RegisterDialog<OrderReturnDialog>();
containerRegistry.RegisterForNavigation<OrderReturnDialog, OrderReturnDialogViewModel>();
// 还空瓶页面
containerRegistry.RegisterForNavigation<ReturnEmptyWindow, ReturnEmptyWindowViewModel>();
// 归还空瓶模态框
containerRegistry.RegisterDialog<ReturnEmptyDialog>();
containerRegistry.RegisterForNavigation<ReturnEmptyDialog, ReturnEmptyDialogViewModel>();
#endregion
#region
// 库存列表页面
containerRegistry.RegisterForNavigation<StockListWindow, StockListWindowViewModel>();
// 库位绑定模态框
//containerRegistry.RegisterDialog<BindingChannelDialog>();
//containerRegistry.RegisterForNavigation<BindingChannelDialog, BindingChannelDialogViewModel>();
// 库位绑定模态框
containerRegistry.RegisterDialog<BindingChannelNewDialog>();
containerRegistry.RegisterForNavigation<BindingChannelNewDialog, BindingChannelNewDialogViewModel>();
// 库存盘点页面
//containerRegistry.RegisterForNavigation<CheckStockWindow, CheckStockWindowViewModel>();
containerRegistry.RegisterDialog<CheckRecordDetailDialog>();
containerRegistry.RegisterForNavigation<CheckRecordDetailDialog, CheckRecordDetailDialogViewModel>();
// 库存盘点页面--新(类似库存列表页面)
containerRegistry.RegisterForNavigation<CheckStockNewWindow, CheckStockNewWindowViewModel>();
// 库存盘点页面--新(类似库存列表页面)
//containerRegistry.RegisterForNavigation<CheckStockNew2Window, CheckStockNew2WindowViewModel>();
// 库存盘点页面--新(类似库存列表页面)
containerRegistry.RegisterForNavigation<CheckRecordNewWindow, CheckRecordNewWindowViewModel>();
// 药品列表页面
containerRegistry.RegisterForNavigation<DrugListWindow, DrugListWindowViewModel>();
//库存列表导出账册弹窗
//containerRegistry.RegisterForNavigation<StockListAccountDialog, StockListAccountDialogViewModel>();
#endregion
#region
// 用户管理页面
containerRegistry.RegisterForNavigation<UserManagerWindow, UserManagerWindowViewModel>();
// 编辑用户模态框
containerRegistry.RegisterDialog<EditUserDialog>();
containerRegistry.RegisterForNavigation<EditUserDialog, EditUserDialogViewModel>();
// 角色管理页面
containerRegistry.RegisterForNavigation<RoleManagerWindow, RoleManagerWindowViewModel>();
// 系统设置
containerRegistry.RegisterForNavigation<SettingWindow, SettingWindowViewModel>();
// 调试页面
containerRegistry.RegisterForNavigation<DebugWindow, DebugWindowViewModel>();
#endregion
// 设备记录页面
containerRegistry.RegisterForNavigation<MachineRecordWindow, MachineRecordWindowViewModel>();
containerRegistry.RegisterForNavigation<ShowMessageDialog, ShowMessageDialogViewModel>();
//交接班
//containerRegistry.RegisterForNavigation<ChangeShiftsWindow, ChangeShiftsWindowViewModel>();
//交接班记录
containerRegistry.RegisterForNavigation<ChangeShiftsListWindow, ChangeShiftsListWindowViewModel>();
containerRegistry.RegisterForNavigation<PrintPdfView, PrintPdfViewModel>();
//交接班
containerRegistry.RegisterForNavigation<ChangeShiftsDialog, ChangeShiftsDialogViewModel>();
//containerRegistry.RegisterSingleton<SqlSugarScope>(() => SqlSugarHelperNew.GetInstance());
#region SqlSugar-IOC单例
//containerRegistry.RegisterSingleton<ISqlSugarClient>(s =>
// {
// SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
// {
// ConnectionString = ConfigurationManager.ConnectionStrings["database"].ToString(),
// DbType = SqlSugar.DbType.MySql,
// IsAutoCloseConnection = true
// },
// db =>
// {
// //单例参数配置,所有上下文生效
// db.Aop.OnLogExecuting = (sql, pars) =>
// {
// //获取IOC对象不要求在一个上下文
// //vra log=s.GetService<Log>()
// //获取IOC对象要求在一个上下文
// //var appServive = s.GetService<IHttpContextAccessor>();
// //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
// };
// });
// return sqlSugar;
// });
#endregion
//账册页面
containerRegistry.RegisterForNavigation<AccountWindow, AccountWindowViewModel>();
//账册服务类
containerRegistry.Register<MachineRecordService>();
//containerRegistry.Register<SqlHelperService>();
// 配置数据库信息
//SCHelper.Database.DBHelper.GetInstance().SetConnectionConfig(SCHelper.Database.SCDbType.MySQL,
// ConfigurationManager.AppSettings["DataBaseIP"],
// ConfigurationManager.AppSettings["DataBaseName"],
// "root",
// ConfigurationManager.AppSettings["Password"]);
}
private void PrismApplication_Startup(object sender, StartupEventArgs e)
{
// 配置数据库信息
//SCHelper.Database.DBHelper.GetInstance().SetConnectionConfig(SCHelper.Database.SCDbType.MySQL,
//"127.0.0.1",
//"hunanxianchang_db",
//"root",
//"root");
2024-07-06 10:01:30 +08:00
//获取欲启动程序名
string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
//检查程序是否已经启动,已经启动则显示提示退出程序
if (System.Diagnostics.Process.GetProcessesByName(processName).Length > 1)
{
//系统在运行
RaiseOtherProcess();
Application.Current.Shutdown();
return;
}
}
private static void RaiseOtherProcess()
{
Process proc = Process.GetCurrentProcess();
foreach (Process otherProc in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
{
if (proc.Id != otherProc.Id)
{
IntPtr hWnd = otherProc.MainWindowHandle;
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, 9);
}
SetForegroundWindow(hWnd);
break;
}
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
//protected IServiceProvider ConfigureServices(IServiceCollection services)
//{
// // 创建 SqlSugar 实例
// var db = new SqlSugarClient(new ConnectionConfig()
// {
// ConnectionString = "server=127.0.0.1;uid=root;pwd=123456;database=mydb",
// DbType = DbType.MySql,
// IsAutoCloseConnection = true,
// InitKeyType = InitKeyType.Attribute
// });
// // 将实例注册到 IServiceCollection 中
// services.AddSingleton(db);
//}
}
}