简体   繁体   English

如何在 WPF .NET 核心应用程序中启用控制台?

[英]How do I enable the console in a WPF .NET Core application?

I'm trying out WPF .NET Core for the first time, and one of the things I always do in a new WPF .NET Framework project is turn on the console, but I receive an error when I try to do this in .NET Core. I'm trying out WPF .NET Core for the first time, and one of the things I always do in a new WPF .NET Framework project is turn on the console, but I receive an error when I try to do this in .NET Core.

In traditional WPF, targeting .NET Framework, this was fairly simple;在传统的 WPF 中,针对 .NET 框架,这相当简单;

  • Set the Build Action for App.xaml to Page将 App.xaml 的 Build Action 设置为 Page
  • Define a Main method somewhere, and tag it with the STAThreadAttribute在某处定义一个 Main 方法,并用 STAThreadAttribute 标记它
  • In the project settings, set the output type to Console Application, and set the Startup object to wherever you put the Main method在项目设置中,将 output 类型设置为 Console Application,并将 Startup object 设置为您放置 Main 方法的任何位置

I replicated these steps in the WPF .NET Core, but I get an error when I try to change the Build Action for App.xaml我在 WPF .NET 核心中复制了这些步骤,但是当我尝试更改 App.xaml 的构建操作时出现错误

The error (it occurs immediately after selecting Page in the dropdown in the Properties window):错误(在属性窗口的下拉列表中选择页面后立即发生):

An error has occurred while saving the edited properties listed below:
    Build Action
One or more values are invalid. 
Cannot add 'App.xaml' to the project, because the path is explicitly excluded from the project 
(C:\Program Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk.WindowsDesktop\targets\Microsoft.NET.Sdk.WindowsDesktop.targets (45,5)).

Does anyone know a way around this, or another way to enable the console in WPF .NET Core?有谁知道解决这个问题的方法,或者在 WPF .NET Core 中启用控制台的另一种方法?

Setting the following properties will make your WPF application a "Console Application"设置以下属性将使您的 WPF 应用程序成为“控制台应用程序”

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>

The SDK automatically change OutputType from Exe to WinExe for WPF and WinForms apps. SDK 自动将 WPF 和 WinForms 应用程序的 OutputType 从Exe更改为WinExe See https://docs.microsoft.com/en-us/dotnet/core/compatibility/windows-forms/5.0/automatically-infer-winexe-output-type请参阅https://docs.microsoft.com/en-us/dotnet/core/compatibility/windows-forms/5.0/automatically-infer-winexe-output-type

After some trial and error, I figured out that the.csproj file got messed up.经过反复试验,我发现 .csproj 文件搞砸了。 I don't know how exactly it went wrong, I suspect it had to do with editing the project through its Properties window in that version of VS2019.我不知道它到底是怎么出错的,我怀疑它与通过该版本 VS2019 中的 Properties window 编辑项目有关。

I edited the.csproj by hand, and it works if it looks as follows:我手动编辑了.csproj,如果它看起来如下所示,它可以工作:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
    <ApplicationIcon />
    <StartupObject>ProjectName.App</StartupObject>
  </PropertyGroup>

  <ItemGroup>
    <Page Include="App.xaml"></Page>
  </ItemGroup>

  <ItemGroup>
    <ApplicationDefinition Remove="App.xaml"></ApplicationDefinition>      <--- key part
  </ItemGroup>

</Project>

I checked the.csproj file for a working .NET Framework project, and the key seems to be removing App.xaml as ApplicationDefinition by hand, as the .NET Framework.csproj did not have that section in it.我检查了 .csproj 文件中是否有一个工作 .NET 框架项目,关键似乎是手动删除 App.xaml 作为 ApplicationDefinition,因为 .NET Framework.csproj 部分没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM