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

Sef*_*Sef 4 c# wpf .net-core

我是第一次尝试 WPF .NET Core,在新的 WPF .NET Framework 项目中我总是做的一件事就是打开控制台,但当我尝试在 .NET 中执行此操作时收到错误核。

在传统的 WPF 中,针对 .NET Framework,这相当简单;

  • 将 App.xaml 的构建操作设置为页面
  • 在某处定义 Main 方法,并使用 STAThreadAttribute 对其进行标记
  • 在项目设置中,将输出类型设置为控制台应用程序,并将启动对象设置为放置 Main 方法的位置

我在 WPF .NET Core 中复制了这些步骤,但当我尝试更改 App.xaml 的构建操作时出现错误

错误(在“属性”窗口的下拉列表中选择“页面”后立即发生):

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)).
Run Code Online (Sandbox Code Playgroud)

有谁知道解决这个问题的方法,或者在 WPF .NET Core 中启用控制台的其他方法?

fjc*_*997 6

设置以下属性将使您的 WPF 应用程序成为“控制台应用程序”

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

对于 WPF 和 WinForms 应用程序,SDK 自动将 OutputType 从 更改Exe为。WinExe请参阅https://learn.microsoft.com/en-us/dotnet/core/compatibility/windows-forms/5.0/automatically-infer-winexe-output-type