From 88ef76bd4d8335097ee2191bfb0404e4a15a971b Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 29 May 2025 08:58:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml | 7 ++++++ App.xaml.cs | 42 +++++++++++++++++++++++++++++++ AssemblyInfo.cs | 10 ++++++++ Data/DbContext.cs | 25 ++++++++++++++++++ Message/MyMessage.cs | 20 +++++++++++++++ Message/ReqMessage.cs | 13 ++++++++++ PMSWPF.csproj | 31 +++++++++++++++++++++++ PMSWPF.sln | 22 ++++++++++++++++ Services/DemoBackgroundService.cs | 37 +++++++++++++++++++++++++++ ViewModels/MainViewModel.cs | 24 ++++++++++++++++++ Views/MainView.xaml | 30 ++++++++++++++++++++++ Views/MainView.xaml.cs | 17 +++++++++++++ 12 files changed, 278 insertions(+) create mode 100644 App.xaml create mode 100644 App.xaml.cs create mode 100644 AssemblyInfo.cs create mode 100644 Data/DbContext.cs create mode 100644 Message/MyMessage.cs create mode 100644 Message/ReqMessage.cs create mode 100644 PMSWPF.csproj create mode 100644 PMSWPF.sln create mode 100644 Services/DemoBackgroundService.cs create mode 100644 ViewModels/MainViewModel.cs create mode 100644 Views/MainView.xaml create mode 100644 Views/MainView.xaml.cs diff --git a/App.xaml b/App.xaml new file mode 100644 index 0000000..ff7637e --- /dev/null +++ b/App.xaml @@ -0,0 +1,7 @@ + + + diff --git a/App.xaml.cs b/App.xaml.cs new file mode 100644 index 0000000..cd89c38 --- /dev/null +++ b/App.xaml.cs @@ -0,0 +1,42 @@ +using System.Configuration; +using System.Data; +using System.Windows; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using PMSWPF.Services; +using PMSWPF.ViewModels; +using PMSWPF.Views; + +namespace PMSWPF +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + [STAThread] + static void Main(string[] args) + { + using IHost host = CreateHostBuilder(args).Build(); + host.Start(); + App app = new App(); + app.InitializeComponent(); + app.MainWindow = host.Services.GetRequiredService(); + app.MainWindow.Visibility = Visibility.Visible; + app.Run(); + + } + + private static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureServices(services => + { + + services.AddHostedService(); + services.AddSingleton(); + services.AddSingleton(); + }); + } + } + +} diff --git a/AssemblyInfo.cs b/AssemblyInfo.cs new file mode 100644 index 0000000..b0ec827 --- /dev/null +++ b/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/Data/DbContext.cs b/Data/DbContext.cs new file mode 100644 index 0000000..a92acd2 --- /dev/null +++ b/Data/DbContext.cs @@ -0,0 +1,25 @@ +using SqlSugar; + +namespace PMSWPF.Data +{ + internal class DbContext + { + private static SqlSugarClient _db; + + public static SqlSugarClient GetInstance() + { + if (_db == null) + { + string connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=PMS;"; + _db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = connectionString, + DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等 + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute + }); + } + return _db; + } + } +} diff --git a/Message/MyMessage.cs b/Message/MyMessage.cs new file mode 100644 index 0000000..d791d4c --- /dev/null +++ b/Message/MyMessage.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace PMSWPF.Message +{ + public class MyMessage : ValueChangedMessage + { + private int count; + + public MyMessage(int value) : base(value) + { + } + + public int Count { get => count; set => count = value; } + } +} diff --git a/Message/ReqMessage.cs b/Message/ReqMessage.cs new file mode 100644 index 0000000..fb7a5ee --- /dev/null +++ b/Message/ReqMessage.cs @@ -0,0 +1,13 @@ +using CommunityToolkit.Mvvm.Messaging.Messages; + +namespace PMSWPF.Message +{ + public class ReqMessage : RequestMessage + { + public int Count { get; set; } + public ReqMessage(int count) + { + Count = count; + } + } +} diff --git a/PMSWPF.csproj b/PMSWPF.csproj new file mode 100644 index 0000000..568a05b --- /dev/null +++ b/PMSWPF.csproj @@ -0,0 +1,31 @@ + + + + WinExe + net8.0-windows + enable + enable + true + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PMSWPF.sln b/PMSWPF.sln new file mode 100644 index 0000000..680f307 --- /dev/null +++ b/PMSWPF.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35728.132 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMSWPF", "PMSWPF.csproj", "{CD3529C9-218C-41EE-B64B-A884DC56E21E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CD3529C9-218C-41EE-B64B-A884DC56E21E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD3529C9-218C-41EE-B64B-A884DC56E21E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD3529C9-218C-41EE-B64B-A884DC56E21E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Services/DemoBackgroundService.cs b/Services/DemoBackgroundService.cs new file mode 100644 index 0000000..e1c28cb --- /dev/null +++ b/Services/DemoBackgroundService.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using Microsoft.Extensions.Hosting; +using PMSWPF.Message; + +namespace PMSWPF.Services +{ + internal class DemoBackgroundService : BackgroundService + { + int count = 0; + + public DemoBackgroundService() + { + + } + + + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + + while (!stoppingToken.IsCancellationRequested) + { + await Task.Delay(1000); + count += 1; + var msg = new MyMessage(35) { Count = count }; + WeakReferenceMessenger.Default.Send(msg); + Console.WriteLine("Hello"); + } + } + } +} diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs new file mode 100644 index 0000000..2b41421 --- /dev/null +++ b/ViewModels/MainViewModel.cs @@ -0,0 +1,24 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using PMSWPF.Message; + +namespace PMSWPF.ViewModels +{ + partial class MainViewModel : ObservableRecipient, IRecipient + { + public MainViewModel() + { + IsActive = true; + } + + string text = "Hello Count:"; + + [ObservableProperty] + string message; + + public void Receive(MyMessage message) + { + Message = text + message.Count; + } + } +} diff --git a/Views/MainView.xaml b/Views/MainView.xaml new file mode 100644 index 0000000..8ccc508 --- /dev/null +++ b/Views/MainView.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs new file mode 100644 index 0000000..e364eda --- /dev/null +++ b/Views/MainView.xaml.cs @@ -0,0 +1,17 @@ +using System.Windows; +using PMSWPF.ViewModels; + +namespace PMSWPF.Views +{ + /// + /// MainView.xaml 的交互逻辑 + /// + public partial class MainView : Window + { + public MainView() + { + InitializeComponent(); + this.DataContext = new MainViewModel(); + } + } +}