【问题标题】:MessageBox in .net core 5.net core 5 中的 MessageBox
【发布时间】:2021-07-07 17:00:11
【问题描述】:

从我读到的 System.Windows.MessageBox 在 .net core 5 中可用 链接:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.messagebox?view=net-5.0

Similar method to messageBox in .NET core

但是,当我尝试使用 System.Windows.MessageBox 时出现错误: 命名空间“System.Windows”中不存在类型或命名空间名称“MessageBox”

我是否缺少设置?

【问题讨论】:

  • 您需要在项目文件中使用<UseWPF>true</UseWPF>添加对WPF的引用
  • 谢谢@jl0pd。我还必须添加 net5.0-windows
  • @PaulMcCarthy net5.0 是跨平台运行时,相当于 .NET Standard。要使用 Windows 桌面功能,您需要以 Windows 运行时为目标。

标签: c# .net-core


【解决方案1】:

假设您要使用 WPF,您的 csproj 文件必须如下所示:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF> <!-- this adds support for the GUI stuff like MessageBox -->
  </PropertyGroup>
</Project>

那么你应该可以在MainWindow.xaml.cs中做这样的事情:

using System.Windows;

namespace messagebox
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            MessageBox.Show("Hello World!");
        }
    }
}

【讨论】:

  • 最重要的部分是net5.0-windows。没有它,就无法使用桌面 API
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 2015-07-29
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
相关资源
最近更新 更多