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="参数配置" /> -