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

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

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

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();
        }
    }
}
<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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
创建ABP框架的空解决方案,可以分为两种方式:使用ABP CLI命令行工具和使用Visual Studio模板。 方式一:使用ABP CLI命令行工具创建空解决方案 1. 安装ABP CLI命令行工具 ABP CLI是ABP框架的命令行工具,用于创建和管理ABP项目。你需要在本地安装ABP CLI命令行工具,可以在命令行中使用以下命令进行安装: ``` dotnet tool install -g Volo.Abp.Cli ``` 2. 创建空解决方案 在命令行中使用以下命令来创建一个空解决方案: ``` abp new MySolutionName ``` 其中,MySolutionName是你要创建的解决方案名称。 3. 选择模板 在创建空解决方案时,ABP CLI会提示你选择模板。你可以选择ASP.NET Core MVC、Blazor等模板。如果只需要一个空解决方案,可以选择Empty模板。 4. 运行应用程序 使用Visual Studio打开解决方案,编译运行应用程序。 方式二:使用Visual Studio模板创建空解决方案 1. 下载ABP框架的Visual Studio模板 你可以从ABP框架官网下载Visual Studio模板,下载完成后安装到Visual Studio中。 2. 创建空解决方案 在Visual Studio中选择File -> New -> Project,在弹出的窗口中选择ABP Solution模板,输入解决方案名称和路径,选择空解决方案模板,点击Create按钮。 3. 运行应用程序 编译运行应用程序,测试解决方案是否创建成功。 以上就是使用ABP CLI命令行工具和Visual Studio模板创建空解决方案的步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为风而战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值