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

开发 后端
最近要做个小工具软件,发现以前用的WPF界面有点老了,所以在网上找下,发现一个用起来还可以的WPFUI库,MVVM也支持得很好,同时支持微软官方的依赖注入框架Microsoft.Extensions.DependencyInjection。

概述:一款名为WPFUI的轻量级开源库,为WPF应用程序提供现代化界面。支持MVVM和Microsoft.Extensions.DependencyInjection,简单上手。无第三方依赖,内置两套皮肤,可自定义样式。适用于一般应用场景,不受MVVM框架限制。通过简单的引用和配置,快速构建现代化WPF应用,提升用户体验

最近要做个小工具软件,发现以前用的WPF界面有点老了,所以在网上找下,发现一个用起来还可以的WPFUI库,MVVM也支持得很好,同时支持微软官方的依赖注入框架Microsoft.Extensions.DependencyInjection。

先来看看运行效果:

使用方法也比较简单。

1、引用库:

2、App.xaml引入资源

<Application
    x:Class="DesktopApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
    DispatcherUnhandledException="OnDispatcherUnhandledException"
    Exit="OnExit"
    Startup="OnStartup">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ui:ThemesDictionary Theme="Dark" />
                <ui:ControlsDictionary />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3、App.xaml.cs注册相关的Page、ViewModel、Service

public partial class App
    {
        // The.NET Generic Host provides dependency injection, configuration, logging, and other services.
        // https://docs.microsoft.com/dotnet/core/extensions/generic-host
        // https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
        // https://docs.microsoft.com/dotnet/core/extensions/configuration
        // https://docs.microsoft.com/dotnet/core/extensions/logging
        private static readonly IHost _host = Host
            .CreateDefaultBuilder()
            .ConfigureAppConfiguration(c => { c.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)); })
            .ConfigureServices((context, services) =>
            {
                services.AddHostedService<ApplicationHostService>();

                services.AddSingleton<MainWindow>();
                services.AddSingleton<MainWindowViewModel>();
                services.AddSingleton<INavigationService, NavigationService>();
                services.AddSingleton<ISnackbarService, SnackbarService>();
                services.AddSingleton<IContentDialogService, ContentDialogService>();

                services.AddSingleton<DashboardPage>();
                services.AddSingleton<DashboardViewModel>();
                services.AddSingleton<DataPage>();
                services.AddSingleton<DataViewModel>();
                services.AddSingleton<SettingsPage>();
                services.AddSingleton<SettingsViewModel>();
            }).Build();

        /// <summary>
        /// Gets registered service.
        /// </summary>
        /// <typeparam name="T">Type of the service to get.</typeparam>
        /// <returns>Instance of the service or <see langword="null"/>.</returns>
        public static T GetService<T>()
            where T : class
        {
            return _host.Services.GetService(typeof(T)) as T;
        }

        /// <summary>
        /// Occurs when the application is loading.
        /// </summary>
        private void OnStartup(object sender, StartupEventArgs e)
        {
            _host.Start();
            Wpf.Ui.Appearance.Theme.Apply(Wpf.Ui.Appearance.ThemeType.Dark);
        }

        /// <summary>
        /// Occurs when the application is closing.
        /// </summary>
        private async void OnExit(object sender, ExitEventArgs e)
        {
            await _host.StopAsync();

            _host.Dispose();
        }

        /// <summary>
        /// Occurs when an exception is thrown by an application but not handled.
        /// </summary>
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            // For more info see https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.dispatcherunhandledexception?view=windowsdesktop-6.0
        }

4、MainWindow页面进行主界面布局

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

5、优点

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

再附上几张效果图:

责任编辑:姜华 来源: 今日头条
相关推荐

2009-09-07 23:09:17

2023-11-16 13:24:39

OceanBase数据库

2024-01-02 08:22:01

Koin框架项目

2023-12-20 07:35:03

大模型数据治理机器学习

2009-07-02 09:32:00

2009-12-03 15:34:41

Suse Linux

2011-05-24 09:26:02

有线无线3G

2009-08-17 22:32:25

IT运维管理监控运维一体化摩卡

2020-04-24 11:13:50

物联网智慧社区技术

2023-06-28 08:16:50

Autofac应用程序

2017-05-16 10:46:06

博阳咨询流程管理

2014-12-25 11:25:31

2023-07-19 22:13:25

一体化推送平台

2009-03-19 09:50:00

华为机房一体化

2014-10-14 10:45:18

用友

2013-11-08 18:01:43

SAP中国商业同略会

2009-02-06 14:12:00

数据中心机房

2014-11-06 10:31:55

移动营销

2013-09-05 13:04:21

用友
点赞
收藏

51CTO技术栈公众号