翼度科技»论坛 编程开发 .net 查看内容

现代WPF界面轻松实现:探秘轻量级WPFUI库,MVVM与依赖注入一体化

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
 
概述:一款名为WPFUI的轻量级开源库,为WPF应用程序提供现代化界面。支持MVVM和
Microsoft.Extensions.DependencyInjection,简单上手。无第三方依赖,内置两套皮肤,可自定义样式。适用于一般应用场景,不受MVVM框架限制。通过简单的引用和配置,快速构建现代化WPF应用,提升用户体验
最近要做个小工具软件,发现以前用的WPF界面有点老了,所以在网上找下,发现一个用起来还可以的WPFUI库,MVVM也支持得很好,同时支持微软官方的依赖注入框架
Microsoft.Extensions.DependencyInjection。
先来看看运行效果:
 
使用方法也比较简单
1、引用库:

 
2、App.xaml引入资源
  1. <Application
  2.     x:
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.     xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
  6.     DispatcherUnhandledException="OnDispatcherUnhandledException"
  7.     Exit="OnExit"
  8.     Startup="OnStartup">
  9.     <Application.Resources>
  10.         <ResourceDictionary>
  11.             <ResourceDictionary.MergedDictionaries>
  12.                 <ui:ThemesDictionary Theme="Dark" />
  13.                 <ui:ControlsDictionary />
  14.             </ResourceDictionary.MergedDictionaries>
  15.         </ResourceDictionary>
  16.     </Application.Resources>
  17. </Application>
复制代码
3、App.xaml.cs注册相关的Page、ViewModel、Service
  1.     public partial class App
  2.     {
  3.         // The.NET Generic Host provides dependency injection, configuration, logging, and other services.
  4.         // https://docs.microsoft.com/dotnet/core/extensions/generic-host
  5.         // https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
  6.         // https://docs.microsoft.com/dotnet/core/extensions/configuration
  7.         // https://docs.microsoft.com/dotnet/core/extensions/logging
  8.         private static readonly IHost _host = Host
  9.             .CreateDefaultBuilder()
  10.             .ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)); })
  11.             .ConfigureServices((context, services) =>
  12.             {
  13.                 services.AddHostedService<ApplicationHostService>();
  14.                 services.AddSingleton<MainWindow>();
  15.                 services.AddSingleton<MainWindowViewModel>();
  16.                 services.AddSingleton<INavigationService, NavigationService>();
  17.                 services.AddSingleton<ISnackbarService, SnackbarService>();
  18.                 services.AddSingleton<IContentDialogService, ContentDialogService>();
  19.                 services.AddSingleton<DashboardPage>();
  20.                 services.AddSingleton<DashboardViewModel>();
  21.                 services.AddSingleton<DataPage>();
  22.                 services.AddSingleton<DataViewModel>();
  23.                 services.AddSingleton<SettingsPage>();
  24.                 services.AddSingleton<SettingsViewModel>();
  25.             }).Build();
  26.         /// <summary>
  27.         /// Gets registered service.
  28.         /// </summary>
  29.         /// <typeparam name="T">Type of the service to get.</typeparam>
  30.         /// <returns>Instance of the service or <see langword="null"/>.</returns>
  31.         public static T GetService<T>()
  32.             where T : class
  33.         {
  34.             return _host.Services.GetService(typeof(T)) as T;
  35.         }
  36.         /// <summary>
  37.         /// Occurs when the application is loading.
  38.         /// </summary>
  39.         private void OnStartup(object sender, StartupEventArgs e)
  40.         {
  41.             _host.Start();
  42.             Wpf.Ui.Appearance.Theme.Apply(Wpf.Ui.Appearance.ThemeType.Dark);
  43.         }
  44.         /// <summary>
  45.         /// Occurs when the application is closing.
  46.         /// </summary>
  47.         private async void OnExit(object sender, ExitEventArgs e)
  48.         {
  49.             await _host.StopAsync();
  50.             _host.Dispose();
  51.         }
  52.         /// <summary>
  53.         /// Occurs when an exception is thrown by an application but not handled.
  54.         /// </summary>
  55.         private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  56.         {
  57.             // For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
  58.         }
复制代码
4、MainWindow页面进行主界面布局

这个代码有点多就不粘了,文章结尾有源代码下载,如果感兴趣可以下载看看。
5、ViewModel、Service的定义

...
6、优点


  • 这个库包含了一些常用的控件没有过多的封装(轻量级),但足够一般应用场景使用
  • 包含了两套皮肤(如果不满意可以自定义样式个性色调)
  • 没有其它第三方的依赖,使用起来比较简单
  • 使用官方Microsoft.Extensions.DependencyInjection作为依赖注入框架,也可以使用其他的
  • 没有MVVM框架的限制,可以使用CommunityToolkit.Mvvm、Prism或其他的
再附上几张效果图:
 
 
 


来源:https://www.cnblogs.com/hanbing81868164/Undeclared/17953594
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具