ozz*_*ald 3 winforms .net-core
如何使用 .NET Core 的通用主机运行 Win Forms 并让它控制主机生命周期?我想使用主机的默认构建器并使用扩展方法来添加我的启动表单。我希望主机在启动表单关闭时关闭。
您可以使用OswaldTechnologies.Extensions.Hosting.WindowsFormsLifetime我编写的库来完成此任务。
安装包:
Install-Package OswaldTechnologies.Extensions.Hosting.WindowsFormsLifetime
Run Code Online (Sandbox Code Playgroud)
Program使用默认 .NET Core Win Forms 模板之前的类:
Install-Package OswaldTechnologies.Extensions.Hosting.WindowsFormsLifetime
Run Code Online (Sandbox Code Playgroud)
Program安装包后将类转换为以下内容:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在 Github 存储库中查看代码:https://github.com/alex-oswald/WindowsFormsLifetime
编辑 2021-11-16该库针对 .NET 6 进行了更新。通过引入最小的 API,使用通用主机启动 Windows 窗体应用程序只需 4 行!
static class Program
{
static void Main()
{
CreateHostBuilder().Build().Run();
}
public static IHostBuilder CreateHostBuilder() =>
Host.CreateDefaultBuilder(Array.Empty<string>())
.UseWindowsFormsLifetime<Form1>()
.ConfigureServices((hostContext, services) =>
{
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2918 次 |
| 最近记录: |