{"id":293,"date":"2026-07-07T08:49:40","date_gmt":"2026-07-07T00:49:40","guid":{"rendered":"https:\/\/www.erwa.cn\/?p=293"},"modified":"2026-07-07T08:49:42","modified_gmt":"2026-07-07T00:49:42","slug":"wpf-%e4%b8%93%e4%b8%9a%e7%ba%a7%e4%ba%a7%e5%93%81%e5%bc%80%e5%8f%91%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97%ef%bc%882026-net8%ef%bc%8c%e5%b7%a5%e4%b8%9a-%e6%a1%8c%e9%9d%a2%e5%95%86%e7%94%a8%e8%bd%af","status":"publish","type":"post","link":"https:\/\/www.erwa.cn\/index.php\/2026\/07\/07\/wpf-%e4%b8%93%e4%b8%9a%e7%ba%a7%e4%ba%a7%e5%93%81%e5%bc%80%e5%8f%91%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97%ef%bc%882026-net8%ef%bc%8c%e5%b7%a5%e4%b8%9a-%e6%a1%8c%e9%9d%a2%e5%95%86%e7%94%a8%e8%bd%af\/","title":{"rendered":"WPF \u4e13\u4e1a\u7ea7\u4ea7\u54c1\u5f00\u53d1\u5b8c\u6574\u6307\u5357\uff082026 .NET8\uff0c\u5de5\u4e1a \/ \u684c\u9762\u5546\u7528\u8f6f\u4ef6\u6807\u51c6\uff09"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">WPF \u4e13\u4e1a\u7ea7\u4ea7\u54c1\u5f00\u53d1\u5b8c\u6574\u6307\u5357\uff082026 .NET8\uff0c\u5de5\u4e1a \/ \u684c\u9762\u5546\u7528\u8f6f\u4ef6\u6807\u51c6\uff09<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u9002\u7528\u573a\u666f\uff1aMES \u4e0a\u4f4d\u673a\u3001\u5de5\u63a7\u5ba2\u6237\u7aef\u3001\u4f01\u4e1a\u540e\u53f0\u3001\u684c\u9762\u5de5\u5177\u3001\u4ed8\u8d39\u5ba2\u6237\u7aef\u8f6f\u4ef6\uff1b\u5927\u5382\u5de5\u4e1a\u8f6f\u4ef6\u7edf\u4e00\u89c4\u8303\uff0c\u517c\u987e\u6027\u80fd\u3001\u53ef\u7ef4\u62a4\u3001\u53ef\u6d4b\u8bd5\u3001\u591a\u4e3b\u9898\u3001\u9ad8 DPI\u3001\u6253\u5305\u90e8\u7f72\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u7840\u9009\u578b\uff08\u4e13\u4e1a\u4ea7\u54c1\u56fa\u5b9a\u65b9\u6848\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u6846\u67b6<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4e3b\u6846\u67b6\uff1a<strong>WPF .NET 8 Windows<\/strong>\uff08\u975e.NET Framework\uff0c\u5355\u6587\u4ef6\u53d1\u5e03\u3001\u88c1\u526a\u4f53\u79ef\u3001\u70ed\u91cd\u8f7d\u3001API \u65b0\u7279\u6027\uff09<\/li>\n\n\n\n<li>\u653e\u5f03 .NET Framework\uff1a\u6253\u5305\u4f53\u79ef\u5927\u3001\u8fd0\u884c\u4f9d\u8d56\u81c3\u80bf\u3001\u65e0\u539f\u751f\u9ad8 DPI \u9002\u914d\u3001\u66f4\u65b0\u9ebb\u70e6<\/li>\n\n\n\n<li>\u5907\u9009 UI \u5e93\uff08\u4e8c\u9009\u4e00\uff0c\u5546\u7528\u514d\u8d39\u65e0\u7248\u6743\u5751\uff09\n<ul class=\"wp-block-list\">\n<li>\u6781\u7b80\u5de5\u4e1a\u98ce\uff1a<strong>HandyControl<\/strong>\uff08\u5f00\u6e90 MIT\uff0c\u65e0\u6388\u6743\u8d39\uff0c\u63a7\u4ef6\u9f50\u5168\u3001\u6697\u8272\u4e3b\u9898\u53cb\u597d\uff0cMES \u4e0a\u4f4d\u673a\u9996\u9009\uff09<\/li>\n\n\n\n<li>\u6807\u51c6\u4f01\u4e1a\u63a7\u4ef6\uff1a<strong>Material Design In XAML (MDIX)<\/strong>\uff0cMD \u98ce\u683c\uff0c\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u597d\u770b<\/li>\n\n\n\n<li>\u4ed8\u8d39\u5546\u4e1a\uff1aDevExpress\/Infragistics\uff08\u5927\u4f01\u4e1a\u6709\u94b1\u624d\u7528\uff0c\u4e2a\u4eba\u5c0f\u4ea7\u54c1\u4e0d\u63a8\u8350\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u672c\u6307\u5357\u4ee5 HandyControl + \u539f\u751f WPF \u4f5c\u4e3a\u6807\u51c6\u6a21\u677f\uff0c\u5de5\u4e1a\u8f6f\u4ef6\u901a\u7528<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u5fc5\u5907 NuGet\uff08\u4e13\u4e1a\u7ea7\u6700\u5c0f\u96c6\u5408\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;!-- WPF\u57fa\u7840\u589e\u5f3a -->\n&lt;PackageReference Include=\"Microsoft.Extensions.DependencyInjection\" Version=\"8.*\" \/>\n&lt;PackageReference Include=\"Microsoft.Extensions.Logging.Serilog\" Version=\"8.*\" \/>\n&lt;PackageReference Include=\"Serilog.Sinks.File\" Version=\"5.*\" \/>\n&lt;!-- MVVM\u6838\u5fc3\uff08\u884c\u4e1a\u6807\u51c6CommunityToolkit.Mvvm\uff0c\u5fae\u8f6f\u5b98\u65b9\uff09 -->\n&lt;PackageReference Include=\"CommunityToolkit.Mvvm\" Version=\"8.*\" \/>\n&lt;!-- UI\u63a7\u4ef6 -->\n&lt;PackageReference Include=\"HandyControl\" Version=\"3.*\" \/>\n&lt;!-- \u914d\u7f6e\u6301\u4e45\u5316\u3001\u672c\u5730\u5b58\u50a8 -->\n&lt;PackageReference Include=\"Microsoft.Extensions.Configuration.Json\" Version=\"8.*\" \/>\n&lt;!-- \u6570\u636e\u5e93\u8bbf\u95ee -->\n&lt;PackageReference Include=\"Microsoft.EntityFrameworkCore.Sqlite\" Version=\"8.*\" \/>\n&lt;!-- \u7a97\u53e3\u62d6\u62fd\u3001\u591a\u663e\u793a\u5668\u3001\u7cfb\u7edf\u70ed\u952e -->\n&lt;PackageReference Include=\"Hardcodet.NotifyIcon.Wpf\" Version=\"1.*\" \/><\/code>\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4e13\u4e1a\u7ea7\u67b6\u6784\uff1aMVVM \u5206\u5c42\uff08\u5355\u4f53\u684c\u9762\uff0c\u4e0d\u62c6\u591a\u89e3\u51b3\u65b9\u6848\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6574\u4f53\u76ee\u5f55\u7ed3\u6784\uff08\u4e25\u683c\u5206\u79bb\u5173\u6ce8\u70b9\uff0c\u53ef\u7ef4\u62a4\u3001\u5355\u5143\u6d4b\u8bd5\u53cb\u597d\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">plaintext<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>MyWpfProduct\/\n\u251c\u2500 Core\/                    # \u6838\u5fc3\u9886\u57df\u5c42\uff08\u65e0WPF\u4f9d\u8d56\uff0c\u7eafC#\uff09\n\u2502  \u251c\u2500 Entities              # \u6570\u636e\u5e93\u5b9e\u4f53\n\u2502  \u251c\u2500 Interfaces            # \u4ed3\u50a8\u3001\u670d\u52a1\u62bd\u8c61\u63a5\u53e3\n\u2502  \u251c\u2500 Exceptions            # \u81ea\u5b9a\u4e49\u4e1a\u52a1\u5f02\u5e38\n\u2502  \u2514\u2500 Models                # \u7eaf\u6570\u636e\u6a21\u578b\uff08DTO\u3001\u914d\u7f6e\u7c7b\uff09\n\u251c\u2500 Services\/                # \u4e1a\u52a1\u670d\u52a1\u5b9e\u73b0\uff08\u4f9d\u8d56Core\uff0c\u4e0d\u4f9d\u8d56UI\uff09\n\u2502  \u251c\u2500 Repository            # EF\u4ed3\u50a8\u5b9e\u73b0\n\u2502  \u251c\u2500 Business              # \u4e1a\u52a1\u903b\u8f91\u670d\u52a1\n\u2502  \u2514\u2500 External              # \u4e32\u53e3\/PLC\/\u7f51\u7edc\u7b2c\u4e09\u65b9\u901a\u4fe1\n\u251c\u2500 AppConfig\/               # \u914d\u7f6e\u3001\u65e5\u5fd7\u3001DI\u5bb9\u5668\u5168\u5c40\u521d\u59cb\u5316\n\u2502  \u251c\u2500 AppSettings.cs\n\u2502  \u251c\u2500 LoggerSetup.cs\n\u2502  \u2514\u2500 ServiceLocator.cs     # DI\u9759\u6001\u5b9a\u4f4d\u5668\uff08\u4ec5\u5f39\u7a97\/\u5168\u5c40\u8c03\u7528\u515c\u5e95\uff09\n\u251c\u2500 ViewModels\/              # MVVM\u89c6\u56fe\u6a21\u578b\uff08\u4ec5\u4f9d\u8d56Core\u3001Services\uff0c\u4e0d\u4f9d\u8d56Window\uff09\n\u2502  \u251c\u2500 MainVm.cs\n\u2502  \u251c\u2500 Pages\/                # \u5206\u9875ViewModel\n\u2502  \u2514\u2500 Dialogs\/              # \u5f39\u7a97ViewModel\n\u251c\u2500 Views\/                   # \u7eafUI\u5c42\uff0c\u53ea\u5199Xaml\uff0c\u65e0\u4e1a\u52a1\u903b\u8f91\n\u2502  \u251c\u2500 Windows\/              # \u4e3b\u7a97\u53e3\u3001\u5f39\u7a97\n\u2502  \u251c\u2500 Pages\/                # \u5bfc\u822a\u9875\u9762UserControl\n\u2502  \u251c\u2500 Controls\/             # \u81ea\u5b9a\u4e49\u590d\u7528\u63a7\u4ef6\n\u2502  \u2514\u2500 Styles\/               # \u5168\u5c40\u8d44\u6e90\u3001\u4e3b\u9898\u3001\u989c\u8272\u6a21\u677f\n\u251c\u2500 Resources\/\n\u2502  \u251c\u2500 Themes\/                # \u660e\u6697\u53cc\u4e3b\u9898\uff08Catppuccin Mocha\u5de5\u4e1a\u6697\u8272\u6807\u51c6\uff09\n\u2502  \u251c\u2500 Icons\/\n\u2502  \u2514\u2500 ResourceDictionary.xaml\n\u251c\u2500 Helpers\/                 # WPF\u5de5\u5177\u7c7b\uff08\u8f6c\u6362\u5668\u3001\u7a97\u53e3\u8f85\u52a9\u3001\u9ad8DPI\uff09\n\u2502  \u251c\u2500 Converters\/\n\u2502  \u251c\u2500 DpiHelper.cs\n\u2502  \u2514\u2500 WindowHelper.cs\n\u251c\u2500 appsettings.json         # \u7a0b\u5e8f\u914d\u7f6e\uff08\u4e32\u53e3\u3001\u6570\u636e\u5e93\u3001\u8bbe\u5907\u53c2\u6570\uff09\n\u2514\u2500 App.xaml \/ App.xaml.cs   # \u7a0b\u5e8f\u5165\u53e3\u3001DI\u6784\u5efa\u3001\u8d44\u6e90\u52a0\u8f7d\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u5c42\u4e25\u683c\u4f9d\u8d56\u89c4\u5219\uff08\u4e13\u4e1a\u8bc4\u5ba1\u7ea2\u7ebf\uff09<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Core<\/strong>\uff1a\u96f6 WPF\u3001\u96f6\u7b2c\u4e09\u65b9 UI \u4f9d\u8d56\uff0c\u4ec5\u57fa\u7840\u7c7b\u578b\uff1b\u53ef\u5355\u72ec\u5355\u5143\u6d4b\u8bd5<\/li>\n\n\n\n<li><strong>Services<\/strong>\uff1a\u4f9d\u8d56 Core\uff0c\u5b9e\u73b0\u6570\u636e\u3001\u8bbe\u5907\u3001\u4e1a\u52a1\uff1b\u5b8c\u5168\u89e3\u8026\u754c\u9762<\/li>\n\n\n\n<li><strong>ViewModel<\/strong>\uff1a\u4f9d\u8d56 Core + Services\uff1b<strong>\u7981\u6b62\u4efb\u4f55 Xaml\u3001Window\u3001\u63a7\u4ef6\u5b9e\u4f8b\u64cd\u4f5c<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u754c\u9762\u4ea4\u4e92\u901a\u8fc7 <code>RelayCommand<\/code>\u3001<code>ObservableObject<\/code>\u3001\u6d88\u606f\u603b\u7ebf\u901a\u4fe1<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>View(Xaml\/Window)<\/strong>\uff1a\u53ea\u7ed1\u5b9a ViewModel\uff0c\u65e0\u4e1a\u52a1\u4ee3\u7801\uff1b\u4ec5\u5904\u7406\u7eaf UI \u89c6\u89c9\u903b\u8f91\uff08\u52a8\u753b\u3001\u5f39\u7a97\u5f39\u51fa\uff09<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001MVVM \u6807\u51c6\u5b9e\u73b0\uff08\u5fae\u8f6f CommunityToolkit.Mvvm \u89c4\u8303\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u7840 ViewModel \u57fa\u7c7b\uff08\u7edf\u4e00\u5c01\u88c5\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">csharp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>using CommunityToolkit.Mvvm.ComponentModel;\nusing CommunityToolkit.Mvvm.Input;\nusing CommunityToolkit.Mvvm.Messaging;\n\nnamespace MyWpfProduct.ViewModels;\n\npublic partial class BaseVm : ObservableObject\n{\n    \/\/ \u6d88\u606f\u603b\u7ebf\uff0c\u9875\u9762\/\u7a97\u53e3\u8de8\u5c42\u901a\u4fe1\uff0c\u4e0d\u76f4\u63a5\u6301\u6709\u5f15\u7528\n    protected readonly IMessenger _messenger;\n\n    public BaseVm(IMessenger messenger)\n    {\n        _messenger = messenger;\n    }\n\n    private bool _isLoading;\n    public bool IsLoading\n    {\n        get => _isLoading;\n        set => SetProperty(ref _isLoading, value);\n    }\n\n    \/\/ \u5168\u5c40\u63d0\u793a\u6d88\u606f\u53d1\u9001\n    protected void SendToast(string msg)\n    {\n        _messenger.Send(new ToastMessage(msg));\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4e3b\u7a97\u53e3 ViewModel \u793a\u4f8b\uff08\u81ea\u52a8\u751f\u6210\u5c5e\u6027\u3001\u547d\u4ee4\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">csharp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>using CommunityToolkit.Mvvm.ComponentModel;\nusing CommunityToolkit.Mvvm.Input;\nusing CommunityToolkit.Mvvm.Messaging;\nusing MyWpfProduct.Core.Interfaces;\nusing MyWpfProduct.Core.Entities;\n\nnamespace MyWpfProduct.ViewModels;\n\n&#91;ObservableObject]\npublic partial class MainVm : BaseVm\n{\n    private readonly IUserRepository _userRepo;\n\n    public MainVm(IMessenger messenger, IUserRepository userRepo) : base(messenger)\n    {\n        _userRepo = userRepo;\n    }\n\n    &#91;ObservableProperty]\n    private string _currentUserName = string.Empty;\n\n    &#91;ObservableProperty]\n    private User? _loginUser;\n\n    \/\/ \u81ea\u52a8\u751f\u6210 IRelayCommand LoadUserCommand\n    &#91;RelayCommand]\n    public async Task LoadUserAsync()\n    {\n        IsLoading = true;\n        try\n        {\n            LoginUser = await _userRepo.GetFirstAsync();\n            CurrentUserName = LoginUser?.UserName ?? \"\u672a\u767b\u5f55\";\n            SendToast(\"\u52a0\u8f7d\u7528\u6237\u5b8c\u6210\");\n        }\n        catch (Exception ex)\n        {\n            SendToast($\"\u52a0\u8f7d\u5931\u8d25\uff1a{ex.Message}\");\n        }\n        finally\n        {\n            IsLoading = false;\n        }\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Xaml \u7ed1\u5b9a\u89c4\u8303\uff08View \u5c42\u53ea\u505a\u7ed1\u5b9a\uff0c\u65e0\u540e\u53f0\u4e1a\u52a1\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;Window x:Class=\"MyWpfProduct.Views.Windows.MainWindow\"\n        xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n        xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n        xmlns:hc=\"https:\/\/handycontrol.io\"\n        Title=\"{Binding CurrentUserName}\" Width=\"1400\" Height=\"900\">\n    &lt;Window.DataContext>\n        &lt;Binding Path=\"MainVm\" Source=\"{StaticResource ServiceLocator}\"\/>\n    &lt;\/Window.DataContext>\n\n    &lt;hc:LoadingMask IsShow=\"{Binding IsLoading}\">\n        &lt;StackPanel>\n            &lt;TextBlock Text=\"\u6b22\u8fce\uff0c{Binding CurrentUserName}\"\/>\n            &lt;Button Content=\"\u52a0\u8f7d\u6570\u636e\" Command=\"{Binding LoadUserCommand}\"\/>\n        &lt;\/StackPanel>\n    &lt;\/hc:LoadingMask>\n&lt;\/Window><\/code>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u540e\u53f0 <code>MainWindow.xaml.cs<\/code> \u7981\u6b62\u5199\u4efb\u4f55\u903b\u8f91\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">csharp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>namespace MyWpfProduct.Views.Windows;\npublic partial class MainWindow\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n    }\n}\n<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001DI \u4f9d\u8d56\u6ce8\u5165\uff08\u4e13\u4e1a\u7ea7\u5168\u5c40\u7edf\u4e00\u7ba1\u7406\uff0c\u675c\u7edd new\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">App.xaml.cs \u5168\u5c40\u5bb9\u5668\u521d\u59cb\u5316\uff08\u6807\u51c6\u6a21\u677f\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">csharp<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>using Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Logging;\nusing CommunityToolkit.Mvvm.Messaging;\nusing MyWpfProduct.Core.Interfaces;\nusing MyWpfProduct.Services.Repository;\nusing MyWpfProduct.ViewModels;\nusing MyWpfProduct.Views.Windows;\nusing Serilog;\n\nnamespace MyWpfProduct;\n\npublic partial class App\n{\n    public static IServiceProvider ServiceProvider { get; private set; } = null!;\n\n    protected override void OnStartup(StartupEventArgs e)\n    {\n        \/\/ 1.\u521d\u59cb\u5316\u65e5\u5fd7\n        LoggerSetup.InitLog();\n\n        \/\/ 2.\u6784\u5efaDI\u5bb9\u5668\n        var services = new ServiceCollection();\n        ConfigureServices(services);\n        ServiceProvider = services.BuildServiceProvider();\n\n        \/\/ 3.\u542f\u52a8\u4e3b\u7a97\u53e3\n        var mainVm = ServiceProvider.GetRequiredService&lt;MainVm>();\n        var mainWin = new MainWindow { DataContext = mainVm };\n        mainWin.Show();\n    }\n\n    private void ConfigureServices(IServiceCollection services)\n    {\n        \/\/ \u65e5\u5fd7\n        services.AddLogging(lb => lb.AddSerilog());\n        \/\/ MVVM\u6d88\u606f\u603b\u7ebf\u5355\u4f8b\n        services.AddSingleton&lt;IMessenger>(WeakReferenceMessenger.Default);\n\n        \/\/ \u4ed3\u50a8\/\u4e1a\u52a1\u670d\u52a1\n        services.AddScoped&lt;IUserRepository, UserRepository>();\n\n        \/\/ \u6240\u6709ViewModel \u6ce8\u5165\n        services.AddTransient&lt;MainVm>();\n    }\n\n    protected override void OnExit(ExitEventArgs e)\n    {\n        Log.CloseAndFlush();\n    }\n}<\/code>\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f3a\u5236\u89c4\u8303<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6240\u6709 ViewModel\u3001\u4ed3\u50a8\u3001\u901a\u4fe1\u670d\u52a1<strong>\u5168\u90e8 DI \u6ce8\u5165<\/strong>\uff0c\u4ee3\u7801\u7981\u6b62\u624b\u52a8<code>new<\/code><\/li>\n\n\n\n<li>\u7a97\u53e3\u3001UserControl \u4e0d\u5728\u6784\u9020\u51fd\u6570 new VM\uff0c\u7edf\u4e00\u7ed1\u5b9a DI \u5bb9\u5668\u5185\u5b9e\u4f8b<\/li>\n\n\n\n<li>\u4e32\u53e3\u3001PLC\u3001\u6570\u636e\u5e93\u8fde\u63a5\u670d\u52a1\u6ce8\u518c\u4e3a<code>Scoped<\/code>\/<code>Singleton<\/code>\u7edf\u4e00\u751f\u547d\u5468\u671f\u7ba1\u7406<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001UI \u4e13\u4e1a\u6807\u51c6\uff08\u5de5\u4e1a\u8f6f\u4ef6\u5fc5\u5907\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u53cc\u4e3b\u9898\u5207\u6362\uff08\u4eae\u8272 \/ \u6697\u8272\uff0cCatppuccin Mocha \u5de5\u4e1a\u6697\u8272\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8d44\u6e90\u76ee\u5f55\u62c6\u5206\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Themes\/LightTheme.xaml<\/code><\/li>\n\n\n\n<li><code>Themes\/DarkTheme.xaml<\/code> \u5168\u5c40\u8d44\u6e90\u5b57\u5178\u52a8\u6001\u52a0\u8f7d\u5207\u6362\uff0cViewModel \u63a7\u5236\u4e3b\u9898\u8d44\u6e90\u91cd\u8f7d\uff0c\u5b9e\u65f6\u751f\u6548\u65e0\u9700\u91cd\u542f\u8f6f\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u9ad8 DPI\u3001\u591a\u663e\u793a\u5668\u9002\u914d\uff08\u5546\u7528\u8f6f\u4ef6\u5fc5\u505a\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9879\u76ee\u6587\u4ef6\u5f00\u542f\u611f\u77e5 DPI\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">xml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;Application xmlns=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\"\n             StartupUri=\"Views\/Windows\/MainWindow.xaml\"\n             DpiAwareness=\"PerMonitorV2\"><\/code>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u5957<code>DpiHelper.cs<\/code>\u81ea\u52a8\u7f29\u653e\u5b57\u4f53\u3001\u63a7\u4ef6\u5c3a\u5bf8\uff0c\u907f\u514d\u9ad8\u5206\u5c4f\u6a21\u7cca\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5168\u5c40\u7edf\u4e00\u6837\u5f0f\u89c4\u8303<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6240\u6709\u6309\u94ae\u3001\u8f93\u5165\u6846\u3001\u8868\u683c\u7edf\u4e00\u8d44\u6e90 Style\uff0c\u4e0d\u91cd\u590d\u5199 Xaml \u6837\u5f0f<\/li>\n\n\n\n<li>\u989c\u8272\u5168\u90e8\u62bd\u79bb\u8d44\u6e90\uff1a<code>PrimaryBrush<\/code>\u3001<code>DangerBrush<\/code>\u3001<code>BackgroundDark<\/code>\u7b49<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u63a7\u4ef6\u653e\u5728<code>Views\/Controls<\/code>\uff0c\u5c01\u88c5\u4f9d\u8d56\u5c5e\u6027\u5bf9\u5916\u66b4\u9732\u7ed1\u5b9a\uff0c\u65e0\u786c\u7f16\u7801<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u7a97\u53e3\u3001\u5f39\u7a97\u6807\u51c6<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f39\u7a97\u7edf\u4e00\u5c01\u88c5<code>DialogService<\/code>\uff0cDI \u6ce8\u5165\uff0cViewModel \u8c03\u7528\u5f39\u7a97\u4e0d\u4f9d\u8d56\u7a97\u53e3\u5b9e\u4f8b<\/li>\n\n\n\n<li>\u6258\u76d8\u901a\u77e5\u4f7f\u7528<code>Hardcodet.NotifyIcon.Wpf<\/code>\uff0c\u6700\u5c0f\u5316\u5230\u6258\u76d8<\/li>\n\n\n\n<li>\u6a21\u6001\u5f39\u7a97\u7edf\u4e00\u5f02\u6b65\u7b49\u5f85\u8fd4\u56de\u7ed3\u679c\uff0c\u652f\u6301\u53d6\u6d88 \/ \u786e\u8ba4\u7ed1\u5b9a\u547d\u4ee4<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6570\u636e\u6301\u4e45\u5316\u4e0e\u8bbe\u5907\u901a\u4fe1\u89c4\u8303\uff08\u5de5\u63a7 \/ \u684c\u9762\u4ea7\u54c1\u6838\u5fc3\uff09<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u672c\u5730\u6570\u636e\u5e93\u9ed8\u8ba4 SQLite<\/strong>\n<ul class=\"wp-block-list\">\n<li>EF Core \u4ed3\u50a8\u5c01\u88c5\uff0cCore \u5b9a\u4e49\u5b9e\u4f53\uff0cServices \u5b9e\u73b0\u4ed3\u50a8<\/li>\n\n\n\n<li>\u5168\u5c40\u8f6f\u5220\u9664\u3001\u5f02\u6b65\u8bfb\u5199\uff0c\u7981\u6b62\u540c\u6b65\u963b\u585e UI<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u4e32\u53e3 \/ PLC\/TCP \u8bbe\u5907\u901a\u4fe1<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u901a\u4fe1\u7c7b\u653e\u5728<code>Services\/External<\/code>\uff0c\u5b9e\u73b0\u63a5\u53e3 <code>IDeviceService<\/code><\/li>\n\n\n\n<li>ViewModel \u4ec5\u8c03\u7528\u63a5\u53e3\uff0c\u4e0d\u6301\u6709\u4e32\u53e3\u53e5\u67c4\uff1b\u5f02\u5e38\u7edf\u4e00\u629b\u4e1a\u52a1\u5f02\u5e38<\/li>\n\n\n\n<li>\u540e\u53f0\u5faa\u73af\u91c7\u96c6\u4f7f\u7528<code>IAsyncEnumerable<\/code>\uff0c\u4e0d\u963b\u585e UI \u7ebf\u7a0b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u914d\u7f6e\u6587\u4ef6 <code>appsettings.json<\/code>\n<ul class=\"wp-block-list\">\n<li>\u4e32\u53e3\u6ce2\u7279\u7387\u3001\u6570\u636e\u5e93\u8def\u5f84\u3001\u8bbe\u5907 IP \u5168\u90e8\u5916\u7f6e\u914d\u7f6e\uff0c\u65e0\u9700\u6539\u4ee3\u7801\u7f16\u8bd1<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u6027\u80fd\u4f18\u5316\u4e13\u4e1a\u89c4\u8303\uff08\u5de5\u4e1a\u4e0a\u4f4d\u673a\u9ad8\u9891\u5237\u65b0\u5fc5\u5907\uff09<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>UI \u7ebf\u7a0b\u7edd\u5bf9\u7981\u6b62\u8017\u65f6\u64cd\u4f5c<\/strong> \u6570\u636e\u5e93\u3001\u4e32\u53e3\u8bfb\u5199\u3001\u6587\u4ef6 IO \u5168\u90e8 <code>async\/await<\/code>\uff1b\u957f\u4efb\u52a1\u540e\u53f0 Task.Run\uff0cUI \u4ec5\u5c55\u793a\u72b6\u6001<\/li>\n\n\n\n<li>\u5927\u6570\u636e\u8868\u683c\u4f18\u5316\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>VirtualizingStackPanel<\/code> \u865a\u62df\u6eda\u52a8\uff0c\u4e07\u884c\u6570\u636e\u4e0d\u5361\u987f<\/li>\n\n\n\n<li>\u9891\u7e41\u5237\u65b0\u5217\u8868\u4f7f\u7528 <code>ObservableRangeCollection<\/code> \u6279\u91cf\u66f4\u65b0\uff0c\u51cf\u5c11\u901a\u77e5\u6b21\u6570<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u56fe\u7247\u8d44\u6e90\u5904\u7406 \u5927\u56fe\u52a0\u8f7d\u4f7f\u7528<code>BitmapImage<\/code>\u5f02\u6b65\u89e3\u7801\uff0c\u91ca\u653e\u5185\u5b58\uff1b\u4e0d\u7528\u65f6\u624b\u52a8\u91ca\u653e\u56fe\u50cf\u8d44\u6e90<\/li>\n\n\n\n<li>\u5185\u5b58\u6cc4\u6f0f\u89c4\u907f\uff08WPF \u5934\u53f7\u5751\uff09\n<ul class=\"wp-block-list\">\n<li>\u6d88\u606f\u603b\u7ebf\u7528\u5b8c\u53d6\u6d88\u8ba2\u9605 <code>_messenger.Unregister(this)<\/code><\/li>\n\n\n\n<li>\u5f39\u7a97\u3001\u9875\u9762\u5173\u95ed\u89e3\u7ed1\u4e8b\u4ef6\u3001\u6e05\u7a7a\u7ed1\u5b9a<\/li>\n\n\n\n<li>\u4e0d\u4f7f\u7528\u9759\u6001\u6301\u6709 Window \/ \u63a7\u4ef6\u5f15\u7528<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u6e32\u67d3\u4f18\u5316 \u9891\u7e41\u5237\u65b0\u533a\u57df\u52a0 <code>RenderOptions.CacheInvalidationThresholdMinimum<\/code> \u7f13\u5b58\u7ed8\u5236<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u65e5\u5fd7\u3001\u5f02\u5e38\u3001\u5168\u5c40\u63d0\u793a\u4ea7\u54c1\u6807\u51c6<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Serilog \u5206\u7ea7\u65e5\u5fd7\uff1aInfo \u6b63\u5e38\u3001Warning \u8bbe\u5907\u5f02\u5e38\u3001Error \u5d29\u6e83\u5806\u6808\uff0c\u6309\u5929\u5206\u5272\u65e5\u5fd7\u6587\u4ef6<\/li>\n\n\n\n<li>\u5168\u5c40\u7edf\u4e00\u4e1a\u52a1\u5f02\u5e38 <code>BusinessException<\/code>\uff0cViewModel \u6355\u83b7\u540e\u53d1\u9001\u5f39\u7a97\u6d88\u606f\uff0c\u4e0d\u76f4\u63a5\u5f39 MessageBox<\/li>\n\n\n\n<li>\u7981\u6b62\u539f\u751f<code>MessageBox.Show()<\/code>\uff0c\u5168\u90e8\u5c01\u88c5\u7edf\u4e00\u63d0\u793a\u63a7\u4ef6\uff08HandyControl Notification\uff09<\/li>\n\n\n\n<li>\u5d29\u6e83\u5168\u5c40\u6355\u83b7\uff0c\u8bb0\u5f55\u5b8c\u6574\u5806\u6808\u65e5\u5fd7\uff0c\u53ef\u5f39\u51fa\u9519\u8bef\u53cd\u9988\u7a97\u53e3<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u6253\u5305\u3001\u53d1\u5e03\u3001\u90e8\u7f72\uff08\u4e13\u4e1a\u4ea4\u4ed8\u65b9\u6848\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. .NET8 \u5355\u6587\u4ef6\u72ec\u7acb\u53d1\u5e03\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">bash<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>dotnet publish -c Release --self-contained true \/p:PublishSingleFile=true \/p:IncludeNativeLibrariesForSelfExtract=true<\/code>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f18\u52bf\uff1a\u6253\u5305\u4ec5\u4e00\u4e2a exe\uff0c\u62f7\u8d1d\u5373\u53ef\u8fd0\u884c\uff0c\u65e0\u9700\u5ba2\u6237\u5b89\u88c5.NET \u8fd0\u884c\u65f6<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5b89\u88c5\u5305\u5236\u4f5c<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8f7b\u91cf\uff1aInno Setup \u514d\u8d39\u6253\u5305\uff0c\u81ea\u5b9a\u4e49\u5b89\u88c5\u76ee\u5f55\u3001\u684c\u9762\u5feb\u6377\u65b9\u5f0f\u3001\u5378\u8f7d\u7a0b\u5e8f<\/li>\n\n\n\n<li>\u4f01\u4e1a\u7ea7\uff1aWiX Toolset MSI \u5b89\u88c5\u5305\uff0c\u652f\u6301\u9759\u9ed8\u90e8\u7f72\u3001\u57df\u5185\u6279\u91cf\u63a8\u9001<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7a0b\u5e8f\u52a0\u56fa<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6df7\u6dc6\uff1aConfuserEx\uff08\u514d\u8d39\uff09\u9632\u6b62\u53cd\u7f16\u8bd1<\/li>\n\n\n\n<li>\u6743\u9650\uff1a\u7ba1\u7406\u5458\u6743\u9650\u914d\u7f6e\u3001Windows \u9632\u706b\u5899\u653e\u884c\u7aef\u53e3\uff08\u5de5\u63a7\u901a\u4fe1\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u7f16\u7801\u7ea2\u7ebf\uff08\u4e13\u4e1a\u9879\u76ee\u4ee3\u7801\u8bc4\u5ba1\u5fc5\u67e5\uff09<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Xaml \u540e\u53f0 cs \u7981\u6b62\u4e1a\u52a1\u903b\u8f91\uff0c\u4ec5\u4fdd\u7559\u521d\u59cb\u5316\u4ee3\u7801<\/li>\n\n\n\n<li>ViewModel \u4e0d\u80fd\u5f15\u7528\u4efb\u4f55 Window\u3001UserControl\u3001Xaml \u63a7\u4ef6\u7c7b\u578b<\/li>\n\n\n\n<li>\u7981\u6b62\u5728 ViewModel \u4e2d<code>new<\/code>\u6570\u636e\u5e93\u3001\u4e32\u53e3\u3001\u65e5\u5fd7\u5b9e\u4f8b\uff0c\u7edf\u4e00 DI<\/li>\n\n\n\n<li>\u6240\u6709 IO\u3001\u8bbe\u5907\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u5fc5\u987b\u5f02\u6b65\uff0c\u7981\u7528<code>.Result<\/code>\u3001<code>.Wait()<\/code>\u963b\u585e UI<\/li>\n\n\n\n<li>\u754c\u9762\u63a7\u4ef6\u5c3a\u5bf8\u3001\u989c\u8272\u7981\u6b62\u786c\u7f16\u7801\u5b57\u7b26\u4e32\uff0c\u5168\u90e8\u5f15\u7528\u8d44\u6e90\u5b57\u5178<\/li>\n\n\n\n<li>\u5927\u6570\u636e\u5217\u8868\u5fc5\u987b\u5f00\u542f\u865a\u62df\u5316\uff0c\u5426\u5219\u89c6\u4e3a\u6027\u80fd\u7f3a\u9677<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u9000\u51fa\u65f6\u5fc5\u987b\u91ca\u653e\u4e32\u53e3\u3001\u6570\u636e\u5e93\u3001\u6258\u76d8\u3001\u65e5\u5fd7\u8d44\u6e90<\/li>\n\n\n\n<li>\u5bc6\u94a5\u3001\u8bbe\u5907\u5bc6\u7801\u7981\u6b62\u786c\u7f16\u7801\uff0c\u5b58\u653e\u914d\u7f6e\u6587\u4ef6\u6216\u52a0\u5bc6\u672c\u5730\u5b58\u50a8<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e00\u3001\u6269\u5c55\u8fed\u4ee3\u8def\u7ebf\uff08\u4ece\u5c0f\u5de5\u5177\u5230\u5546\u7528\u5de5\u4e1a\u8f6f\u4ef6\uff09<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521d\u671f\uff1a\u5355\u9879\u76ee WPF\uff0cSQLite \u672c\u5730\u5b58\u50a8\uff0cHandyControl \u57fa\u7840\u754c\u9762<\/li>\n\n\n\n<li>\u8bbe\u5907\u589e\u591a\uff1a\u62bd\u79bb\u8bbe\u5907\u901a\u4fe1\u670d\u52a1\uff0c\u589e\u52a0\u65ad\u7ebf\u91cd\u8fde\u3001\u6570\u636e\u7f13\u5b58<\/li>\n\n\n\n<li>\u591a\u5ba2\u6237\u7aef\u8054\u52a8\uff1a\u65b0\u589e WebAPI \u670d\u52a1\uff0cWPF \u4f5c\u4e3a\u5ba2\u6237\u7aef\u5bf9\u63a5\u540e\u7aef<\/li>\n\n\n\n<li>\u6743\u9650\u590d\u6742\uff1a\u589e\u52a0 RBAC \u89d2\u8272\u6a21\u5757\uff0c\u9875\u9762\u6309\u94ae\u52a8\u6001\u6743\u9650\u63a7\u5236<\/li>\n\n\n\n<li>\u91cf\u4ea7 MES\uff1a\u589e\u52a0 OPC UA \u5bf9\u63a5\u3001\u6570\u636e\u4e0a\u62a5\u3001\u62a5\u8868\u5bfc\u51fa\u3001\u6253\u5370\u6a21\u677f<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>WPF \u4e13\u4e1a\u7ea7\u4ea7\u54c1\u5f00\u53d1\u5b8c\u6574\u6307\u5357\uff082026 .NET8\uff0c\u5de5\u4e1a \/ \u684c\u9762\u5546\u7528\u8f6f\u4ef6\u6807\u51c6\uff09 \u9002\u7528\u573a\u666f\uff1aMES \u4e0a\u4f4d\u673a\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,25],"tags":[12,24],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-13","category-25","tag-12","tag-24"],"_links":{"self":[{"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":1,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/posts\/293\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.erwa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}