From 05b41a56854ee6d2ad50c4a9f6e4995a8770dbda Mon Sep 17 00:00:00 2001 From: lihanbo Date: Mon, 21 Oct 2024 15:21:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `App.xaml.cs` 中,调整了命名空间的格式,并重新排列了代码块,使其更具可读性。 在 `MainWindow.xaml.cs` 中,添加了 `WeakReferenceMessenger.Default.Register` 的注册。 在 `HostExtension.cs` 中,调整了命名空间的格式,并将 `HostExtension` 类从内部类改为静态类,同时优化了 `AddViewAndViewModel` 方法的实现。 在 `ImportExcelPage.xaml` 中,添加了新的命名空间引用 `converter`,并添加了 `BooleanToVisibilityConverter` 和 `ReBooleanToVisibilityConverter` 转换器。同时,添加了新的 `LoadingMask` DataTemplate 和相关的 `ContentPresenter`,并对现有的控件属性进行了调整和优化。 在 `ImportExcelPage.xaml.cs` 中,删除了一些注释代码,并调整了命名空间的格式。 在 `ImportViewModel.cs` 中,添加了新的命名空间引用,并重构了 `ImportViewModel` 类,添加了异步方法 `ReadForExcel` 和 `GetSheets`,以及新的命令 `OnReLoadExcel` 和 `OnReadSheets`。同时,调整了属性的定义和初始化。 新增了 `BooleanConverter.cs` 文件,定义了一个通用的布尔值转换器类 `BooleanConverter`。 新增了 `ReBooleanToVisibilityConverter.cs` 文件,定义了一个反转的布尔值到可见性转换器类 `ReBooleanToVisibilityConverter`。 --- App.xaml.cs | 61 ++-- Converter/BooleanConverter.cs | 35 +++ Converter/ReBooleanToVisibilityConverter.cs | 12 + MainWindow.xaml.cs | 1 + Utils/HostExtension.cs | 37 ++- Views/Pages/ImportExcelPage.xaml | 81 ++++- Views/Pages/ImportExcelPage.xaml.cs | 4 - Views/ViewModels/ImportViewModel.cs | 311 ++++++++++++-------- 8 files changed, 353 insertions(+), 189 deletions(-) create mode 100644 Converter/BooleanConverter.cs create mode 100644 Converter/ReBooleanToVisibilityConverter.cs diff --git a/App.xaml.cs b/App.xaml.cs index f13a95d..73111ac 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -5,42 +5,39 @@ using ExcelHelper.Utils; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -namespace ExcelHelper +namespace ExcelHelper; + +/// +/// App.xaml 的交互逻辑 +/// +public partial class App : Application { - /// - /// App.xaml 的交互逻辑 - /// - public partial class App : Application + + public static App AppHost; + private readonly IHost _host = Host.CreateDefaultBuilder() + .AddViewAndViewModel() + .ConfigureServices((context, services) => + { + services.AddHostedService(); + services.AddSingleton(); + }) + .Build(); + protected override void OnStartup(StartupEventArgs e) { + AppHost = this; - public static App AppHost; - private readonly IHost _host = Host.CreateDefaultBuilder() - .AddViewAndViewModel() - .ConfigureServices((context, services) => - { - services.AddHostedService(); - services.AddSingleton(); + base.OnStartup(e); + // 初始化AppHost + _host.Start(); + } - }) - .Build(); - protected override void OnStartup(StartupEventArgs e) - { - AppHost = this; - - base.OnStartup(e); - - // 初始化AppHost - _host.Start(); - } - - public T Get() - { - return _host.Services.GetService(); - } - public object Get(Type type) - { - return _host.Services.GetService(type); - } + public T Get() + { + return _host.Services.GetService(); + } + public object Get(Type type) + { + return _host.Services.GetService(type); } } diff --git a/Converter/BooleanConverter.cs b/Converter/BooleanConverter.cs new file mode 100644 index 0000000..2d63223 --- /dev/null +++ b/Converter/BooleanConverter.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Windows.Data; + +namespace ExcelHelper.Converter; +public class BooleanConverter : IValueConverter +{ + protected BooleanConverter(T tValue, T fValue) + { + True = tValue; + False = fValue; + } + + public T True + { + get; set; + } + + public T False + { + get; set; + } + + + public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return value is bool flag && flag ? True : False; + } + + public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return value is T flag && EqualityComparer.Default.Equals(flag, True); + } +} \ No newline at end of file diff --git a/Converter/ReBooleanToVisibilityConverter.cs b/Converter/ReBooleanToVisibilityConverter.cs new file mode 100644 index 0000000..21fbda8 --- /dev/null +++ b/Converter/ReBooleanToVisibilityConverter.cs @@ -0,0 +1,12 @@ +using System.Windows; +using System.Windows.Data; + +namespace ExcelHelper.Converter; +/// +/// BooleanToVisibilityConverter 反转 +/// +[ValueConversion(typeof(bool), typeof(Visibility))] +public class ReBooleanToVisibilityConverter : BooleanConverter +{ + public ReBooleanToVisibilityConverter() : base(Visibility.Visible, Visibility.Collapsed) { } +} \ No newline at end of file diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 9351b3a..b3c6772 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -18,6 +18,7 @@ namespace ExcelHelper InitializeComponent(); DataContext = ViewModel = viewModel; navigationService.InitForFrame(MainFrame); + WeakReferenceMessenger.Default.Register(this, (r, message) => { if (message.Value != null) diff --git a/Utils/HostExtension.cs b/Utils/HostExtension.cs index b39ad5e..661e871 100644 --- a/Utils/HostExtension.cs +++ b/Utils/HostExtension.cs @@ -3,30 +3,29 @@ using ExcelHelper.Views; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -namespace ExcelHelper.Utils +namespace ExcelHelper.Utils; + +internal static class HostExtension { - internal static class HostExtension + public static IHostBuilder AddViewAndViewModel(this IHostBuilder hostBuilder) { - public static IHostBuilder AddViewAndViewModel(this IHostBuilder hostBuilder) + // Scan all assemblies to find all types that implement IViewModel + var assem = Assembly.GetExecutingAssembly(); + var types = assem.GetTypes(); + foreach (var type in types) { - // Scan all assemblies to find all types that implement IViewModel - var assem = Assembly.GetExecutingAssembly(); - var types = assem.GetTypes(); - foreach (var type in types) + if (type.IsInterface || type.IsAbstract) { - if (type.IsInterface || type.IsAbstract) - { - continue; - } - if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type)) - { - hostBuilder.ConfigureServices((context, services) => - { - services.AddSingleton(type); - }); - } + continue; + } + if (typeof(IViewModel).IsAssignableFrom(type) || typeof(IView).IsAssignableFrom(type)) + { + hostBuilder.ConfigureServices((context, services) => + { + services.AddSingleton(type); + }); } - return hostBuilder; } + return hostBuilder; } } diff --git a/Views/Pages/ImportExcelPage.xaml b/Views/Pages/ImportExcelPage.xaml index 959d857..1c09ed8 100644 --- a/Views/Pages/ImportExcelPage.xaml +++ b/Views/Pages/ImportExcelPage.xaml @@ -2,11 +2,12 @@ x:Class="ExcelHelper.Views.Pages.ImportExcelPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:converter="clr-namespace:ExcelHelper.Converter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:local="clr-namespace:ExcelHelper.Views.Pages" - xmlns:utils="clr-namespace:ExcelHelper.Utils" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:utils="clr-namespace:ExcelHelper.Utils" xmlns:viewmodels="clr-namespace:ExcelHelper.Views.ViewModels" Title="ImportExcelPage" d:DataContext="{d:DesignInstance Type=viewmodels:ImportViewModel}" @@ -14,6 +15,8 @@ d:DesignWidth="1200" mc:Ignorable="d"> + + + + + + + + + @@ -39,16 +55,39 @@ Grid.Column="0" Grid.ColumnSpan="2" Panel.ZIndex="10" + Content="{Binding}" ContentTemplate="{StaticResource Mask}" Visibility="Collapsed" /> + - + - - - + + + @@ -67,7 +106,10 @@ Margin="5,0" VerticalAlignment="Center" Text="参数配置" /> -