如何使用 .NET Core 3 和 Visual Studio 创建 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52447586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:10:55  来源:igfitidea点击:

How to create a WPF app with .NET Core 3 and Visual Studio

wpfvisual-studio.net-core

提问by Dean Kuga

What is or will be the correct way of creating WPF applications with .NET Core 3 and WPF Desktop Pack with Visual Studio 2017?

使用 Visual Studio 2017 使用 .NET Core 3 和 WPF Desktop Pack 创建 WPF 应用程序的正确方法是什么或将是什么?

Will there be a new project template in the New Project window or will this be done through NuGet or some other way?

“新建项目”窗口中是否会有新的项目模板,还是通过 NuGet 或其他方式完成?

回答by Dean Kuga

Brian Lagunasrecently posted Getting Started with .NET Core 3 – Create a WPF Applicationvideo on his website which provides detailed instructions on how to create a new WPF application targeting .NET Core 3.

Brian Lagunas最近在他的网站上发布了 .NET Core 3 入门 – 创建 WPF 应用程序视频,其中提供了有关如何创建面向 .NET Core 3 的新 WPF 应用程序的详细说明。

Here is the short of it:

这是它的简短内容:

  • Install .NET Core 3 and SDK:
  • 安装 .NET Core 3 和 SDK:

https://github.com/dotnet/core-setup

https://github.com/dotnet/core-setup

https://github.com/dotnet/core-sdk

https://github.com/dotnet/core-sdk

  • Create new WPF Application targeting .NET Framework.

  • Unload project.

  • Edit csproj file by removing everything and replacing it with the following:

  • 创建面向 .NET Framework 的新 WPF 应用程序。

  • 卸载项目。

  • 通过删除所有内容并将其替换为以下内容来编辑 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWPF>true</UseWPF>
  <AssemblyName>MyCore3App</AssemblyName>
  </PropertyGroup>  
</Project>
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWPF>true</UseWPF>
  <AssemblyName>MyCore3App</AssemblyName>
  </PropertyGroup>  
</Project>
  • Reload project.

  • Build project.

  • If the build fails and your Output window is mentioning duplicate attributes delete the Properties folder.

  • 重新加载项目。

  • 构建项目。

  • 如果构建失败并且您的输出窗口提到重复的属性,请删除 Properties 文件夹。

Watch the video for additional valuable information and keep in mind that .net Core 3 is still in development and these instructions could become inaccurate at any time. I will update the answer as I become aware of any changes impacting this process.

观看视频了解更多有价值的信息,并记住 .net Core 3 仍在开发中,这些说明随时可能变得不准确。当我意识到影响此过程的任何更改时,我将更新答案。

Update 2018-12-05:

2018-12-05 更新

.NET Core 3 Preview 1 was released yesterday, here are the important bits from the announcement:

.NET Core 3 Preview 1 于昨天发布,以下是公告中的重要部分:

  • Visual Studio 2019 will be the release to support building .NET Core 3 applications.

  • Windows Desktop Frameworks are now Open Source and the first wave of source code is already available on GitHub.

  • You can create new .NET Core projects for WPF and Windows Forms from the command line.

  • Visual Studio 2019 将是支持构建 .NET Core 3 应用程序的版本。

  • Windows 桌面框架现在是开源的,第一波源代码已经在 GitHub 上可用。

  • 您可以从命令行为 WPF 和 Windows 窗体创建新的 .NET Core 项目。

dotnet new wpf

dotnet 新 wpf

  • You can also open, launch and debug WPF and Windows Forms projects in Visual Studio 2019 Preview 1. It is currently possible to open .NET Core 3.0 projects in Visual Studio 2017 15.9, however, it is not a supported scenario (and you need to enable previews).

  • csproj file format shown above is still valid.

  • dotnet build now copies NuGet dependencies for your application from the NuGet cache to your build output folder during the build operation. Until this release,those dependencies were only copied as part of dotnet publish. This change allows you to xcopy your build output to different machines.

  • 您还可以在 Visual Studio 2019 Preview 1 中打开、启动和调试 WPF 和 Windows Forms 项目。目前可以在 Visual Studio 2017 15.9 中打开 .NET Core 3.0 项目,但是,它不是受支持的方案(并且您需要启用预览)。

  • 上面显示的 csproj 文件格式仍然有效。

  • dotnet build 现在在生成操作期间将应用程序的 NuGet 依赖项从 NuGet 缓存复制到生成输出文件夹。在此版本之前,这些依赖项仅作为 dotnet publish 的一部分进行复制。此更改允许您将构建输出 xcopy 到不同的机器。

Update 2019-03-15:

2019-03-15 更新

Recently released Visual Studio 2019 RC has a project template for both WPF and Windows Forms targeting .NET Core platform making it very easy to create new WPF and Windows Forms .Net Core applications.

最近发布的 Visual Studio 2019 RC 有一个面向 .NET Core 平台的 WPF 和 Windows Forms 项目模板,这使得创建新的 WPF 和 Windows Forms .Net Core 应用程序变得非常容易。

Update 2019-11-04:

2019-11-04 更新

.NET Core 3.0was released on 2019-09-23.

.NET Core 3.0于 2019 年 9 月 23 日发布。

WPF is now fully supported and .NET Core WPF App project template is available within the "Create a new project" dialog.

现在完全支持 WPF,并且 .NET Core WPF App 项目模板在“创建新项目”对话框中可用。

Project properties of special interest are PublishReadyToRun, PublishSingleFile, and RuntimeIdentifier.

特别感兴趣的项目属性是PublishReadyToRunPublishSingleFileRuntimeIdentifier

This is what a typicall PropertyGroup section would look like with all three options:

这是具有所有三个选项的典型 PropertyGroup 部分的样子:

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWPF>true</UseWPF>
  <PublishReadyToRun>true</PublishReadyToRun>
  <PublishSingleFile>true</PublishSingleFile>
  <RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

These options will allow you to make your WPF Core application a completely self-contained, 64-bit, fast startup executable that will run on any 64-bit version of Windows (just omit the win-x64 runtime identifier if you need it to run on Windows x86) from any location including USB drives.

这些选项将使您的 WPF Core 应用程序成为一个完全独立的 64 位快速启动可执行文件,可以在任何 64 位版本的 Windows 上运行(如果您需要运行它,只需省略 win-x64 运行时标识符)在 Windows x86 上)从任何位置(包括 USB 驱动器)。

The most fantastic thing about WPF Core for all of us Windows desktop developers is that this completely eliminates all the "deployment blues" of the past. You no longer have to worry about what OS the end user is running, what version of .NET framework is available on the target machine or if the user has sufficient permissions to install your app. You can distribute a single file that will run from %LOCALAPPDATA% folder or any location user chooses and it will all just work.

对于我们所有的 Windows 桌面开发人员来说,WPF Core 最美妙的事情是它完全消除了过去的所有“部署蓝调”。您不再需要担心最终用户正在运行什么操作系统、目标计算机上可用的 .NET 框架版本是什么,或者用户是否有足够的权限来安装您的应用程序。您可以分发将从 %LOCALAPPDATA% 文件夹或用户选择的任何位置运行的单个文件,它会正常工作。

Because the resulting executable will contain the .NET Core itself and all the referenced libraries, this will result in a rather large file but it is a small price to pay for all the benefits you get from creating a single self-contained executable. When published like this, my most recent app was 175 MB which I don't consider large at all in 2020.

因为生成的可执行文件将包含 .NET Core 本身和所有引用的库,所以这将产生一个相当大的文件,但为您从创建单个自包含可执行文件中获得的所有好处付出的代价很小。像这样发布时,我最近的应用程序是 175 MB,我认为在 2020 年根本不算大。

However, if you do find the file too large for your liking, you can experiment with a tool called Warpand PublishTrimmed property which is supposed to reduce the file size by analyzing your code and only including portions of the framework used in your application. This couldlead to excluding some required pieces, however, resulting in a non-working executable, especially if your code is using reflection. There is a great blog postby Scott Hanselmanout there going over all of this in more detail.

但是,如果您确实发现文件太大而不适合您的喜好,您可以尝试使用名为Warp和 PublishTrimmed 属性的工具,该工具旨在通过分析您的代码并仅包含应用程序中使用的框架部分来减小文件大小。但是,这可能会导致排除一些必需的部分,从而导致无法运行的可执行文件,尤其是在您的代码使用反射的情况下。Scott Hanselman有一篇很棒的博客文章,其中更详细地介绍了所有这些内容。