Abp 创建一个WPF的项目

开发环境:VS2022、.NET6

1、创建项目:MyWpfApp,这里不再废话了。

2、NuGet添加:

2.1、Volo.Abp.Autofac

2.2、Serilog.Sinks.File

2.3、Serilog.Sinks.Async

2.4、Serilog.Extensions.Logging

2.5、Serilog.Extensions.Hosting

2.6、Microsoft.Extensions.Hosting

,如下图所示:

3、开始写代码

3.1、创建MyWpfAppModule.cs

cs 复制代码
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace MyWpfApp
{
    [DependsOn(typeof(AbpAutofacModule))]
    internal class MyWpfAppModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddSingleton<MainWindow>();
        }
    }
}

3.2、创建 HelloWorldService.cs

cs 复制代码
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace MyWpfApp
{
    public class HelloWorldService:ITransientDependency
    {
        public ILogger<HelloWorldService> Logger { get; set; }

        public HelloWorldService()
        {
            Logger=NullLogger<HelloWorldService>.Instance;
        }

        public string SayHello()
        {
            Logger.LogInformation("Call SayHello");
            return "Hello world";
        }
    }
}

3.3、修改MainWindow.xaml.cs和MainWindow.xaml

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly HelloWorldService _helloWorldService;
        public MainWindow()
        {
            _helloWorldService = new HelloWorldService();
            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            HelloLabel.Content = _helloWorldService.SayHello();
        }
    }
}
cs 复制代码
<Window x:Class="MyWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyWpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Label Name="HelloLabel" FontSize="90" Margin="58,129,-58,-129"/>
    </Grid>
</Window>

3.4、修改App.xaml.cs

cs 复制代码
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Volo.Abp;

namespace MyWpfApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private IAbpApplicationWithInternalServiceProvider? _abpApplication;

        protected override async void OnStartup(StartupEventArgs e)
        {
            Log.Logger = new LoggerConfiguration()
#if DEBUG
                .MinimumLevel.Debug()
#else
                .MinimumLevel.Information()
#endif
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .Enrich.FromLogContext()
                .WriteTo.Async(c => c.File("Logs/logs.txt"))
                .CreateLogger();

            try
            {
                Log.Information("Starting WPF host");

                _abpApplication = await AbpApplicationFactory.CreateAsync<MyWpfAppModule>(options =>
                {
                    options.UseAutofac();
                    options.Services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
                });

                await _abpApplication.InitializeAsync();//此处注释由于_abpApplication未初始化,OnExit会报错

                //_abpApplication.Services.GetRequiredService<MainWindow>()?.Show(); //这里解除注释后会弹出两个MainWindow界面
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly!");
            }
        }

        protected override async void OnExit(ExitEventArgs e)
        {
            if (_abpApplication != null)
            {
                await _abpApplication.ShutdownAsync();
            }

            Log.CloseAndFlush();
        }
    }
}
相关推荐
暮雪倾风2 小时前
【WPF开发】超级详细的“文件选择”(附带示例工程)
windows·wpf
明耀6 小时前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风8 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技1 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆2 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退2 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu2 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退6 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆7 天前
WPF中的XAML详解
wpf·xaml