Skip to content

Commit

Permalink
简化HostBuilder代码
Browse files Browse the repository at this point in the history
  • Loading branch information
c longc committed Aug 1, 2023
1 parent 1b0c39f commit 57ef64e
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DragonKing/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DragonKing"
xmlns:vm="clr-namespace:DragonKing.ViewModel"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Startup="Application_Startup">
<Application.Resources>
Expand All @@ -14,7 +15,6 @@
<ResourceDictionary Source="/DragonKing.UI;component/Collection.xaml" />
<ResourceDictionary Source="/DragonKing.UI;component/Themes/Light.xaml" />
</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>
</Application.Resources>
</Application>
4 changes: 4 additions & 0 deletions DragonKing/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using DragonKing.HostBuilders;
using DragonKing.UI.Utils;
using DragonKing.View;
using DragonKing.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace DragonKing
{
Expand Down
1 change: 1 addition & 0 deletions DragonKing/DragonKing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IHostBuilder AddDbContext(this IHostBuilder host)
{
host.ConfigureServices(services =>
{
services.AddSingleton<UserDbContext>(s => new UserDbContext(s.GetRequiredService<ILog>()));
services.AddSingleton(s => new UserDbContext(s.GetRequiredService<ILog>()));

});

Expand Down
7 changes: 4 additions & 3 deletions DragonKing/HostBuilders/AddViewModelsHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public static IHostBuilder AddViewModels(this IHostBuilder host)
{
host.ConfigureServices(services =>
{
services.AddSingleton<LoginViewModel>(s => new LoginViewModel(s.GetRequiredService<ILog>(), s.GetRequiredService<IUserService>()));
services.AddSingleton<MainViewModel>(s => new MainViewModel(s.GetRequiredService<IUserService>(), s.GetRequiredService<IRoleService>()));
services.AddSingleton<UserManagementViewModel>(s => new UserManagementViewModel(s.GetRequiredService<ILog>(), s.GetRequiredService<IUserService>(), s.GetRequiredService<IRoleService>()));
services.AddSingleton(s => new LoginViewModel(s.GetRequiredService<ILog>(), s.GetRequiredService<IUserService>()));
services.AddSingleton(s => new MainViewModel(s.GetRequiredService<IUserService>(), s.GetRequiredService<IRoleService>()));
services.AddSingleton(s => new UserManagementViewModel(s.GetRequiredService<ILog>(), s.GetRequiredService<IUserService>(), s.GetRequiredService<IRoleService>()));
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<ResultViewModel>();
services.AddSingleton<TestViewModel>();

});

Expand Down
1 change: 1 addition & 0 deletions DragonKing/HostBuilders/AddViewsHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static IHostBuilder AddViews(this IHostBuilder host)
services.AddSingleton<UserManagementView>();
services.AddSingleton<SettingsView>();
services.AddSingleton<ResultView>();
services.AddSingleton<Test>();
});

return host;
Expand Down
4 changes: 2 additions & 2 deletions DragonKing/View/ResultView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DragonKing.ViewModel;
using DragonKing.UI.Utils;
using DragonKing.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,6 +29,5 @@ public ResultView()
this.DataContext = App.Current._host.Services.GetService<ResultViewModel>();

}

}
}
1 change: 1 addition & 0 deletions DragonKing/View/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FontWeight="Bold"
Text="报告设置" />
</TabItem.Header>

</TabItem>
</TabControl>

Expand Down
5 changes: 4 additions & 1 deletion DragonKing/View/SettingsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DragonKing.ViewModel;
using DragonKing.UI.Utils;
using DragonKing.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,5 +29,7 @@ public SettingsView()
this.DataContext = App.Current._host.Services.GetService<SettingsViewModel>();

}


}
}
40 changes: 38 additions & 2 deletions DragonKing/ViewModel/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
using DragonKing.Database.EntityModel;
using DragonKing.Database.Interface;
using DragonKing.Log.Interface;
using DragonKing.UI.Utils;
using DragonKing.Utils;
using DragonKing.View;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace DragonKing.ViewModel
{
Expand Down Expand Up @@ -76,8 +79,6 @@ public void SignIn()
{
User user = _userService.GetUserByName(Username);



if (user != null && user.Password == Password)
{
#region 记住登录信息
Expand All @@ -92,6 +93,41 @@ public void SignIn()

var mainView = App.Current._host.Services.GetRequiredService<MainView>();
mainView.DataContext = App.Current._host.Services.GetRequiredService<MainViewModel>();
/*mainView.Loaded += ((s, e) =>
{
try
{
double screenscale = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth;
// 获取窗体中的所有控件
IEnumerable<FrameworkElement> controls = WPFUtil.GetChildControls(mainView);
foreach (FrameworkElement control in controls)
{
*//*// 排除不需要缩放的控件类型,例如 Label 等
if (!(control is Button) && !(control is TextBox) && !(control is ComboBox))
{
continue;
}*//*
// 将控件的宽度和高度缩放
control.Width /= screenscale;
control.Height /= screenscale;
// 如果是字体大小可以缩放的控件,也可以进行缩放
if (control is Control controlWithFont)
{
controlWithFont.FontSize /= screenscale;
}
}
}
catch (Exception ex)
{
// 错误处理:在控制台输出错误信息
Console.WriteLine($"Error while scaling controls: {ex.Message}");
}
});*/

mainView!.Show();

App.Current._host.Services.GetRequiredService<LoginView>().Close();
Expand Down
2 changes: 1 addition & 1 deletion DragonKing/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void SelectionChanged(object listboxitem)
switch (viewname)
{
case "主页":
Content = App.Current._host.Services.GetService<UserManagementView>();
Content = App.Current._host.Services.GetService<Test>();
break;
case "结果查询":
Content = App.Current._host.Services.GetService<ResultView>();
Expand Down
24 changes: 24 additions & 0 deletions SharedLibraries/DragonKing.UI/Utils/WPFUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,29 @@ public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObj

return null;
}

// 递归获取窗体中的所有子控件
public static IEnumerable<FrameworkElement> GetChildControls(DependencyObject parent)
{
List<FrameworkElement> controls = new List<FrameworkElement>();

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);

if (child is FrameworkElement frameworkElement)
{
controls.Add(frameworkElement);

// 如果该子元素还包含子元素,则继续获取子元素的子元素
if (VisualTreeHelper.GetChildrenCount(child) > 0)
{
controls.AddRange(GetChildControls(child));
}
}
}

return controls;
}
}
}

0 comments on commit 57ef64e

Please sign in to comment.