Mer*_*RAN 5 messagebox console-application .net-core
我正在开发 .net core 控制台应用程序。我想在想要退出应用程序时提醒用户。像下面这样;
MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
Application.Exit();
Run Code Online (Sandbox Code Playgroud)
但我无法将 System.Windows.Forms 引用添加到我的项目中。我收到这个错误。
Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
是否可以在.net core控制台应用程序上显示消息框?
为了使用 Windows 窗体,您需要修改.csproj:
UseWindowsFormstrue-windows到TargetFramework(例如net6.0-windows)ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">\n <PropertyGroup>\n <OutputType>Exe</OutputType>\n <TargetFramework>net6.0-windows</TargetFramework>\n <UseWindowsForms>true</UseWindowsForms>\n </PropertyGroup>\n</Project>\nRun Code Online (Sandbox Code Playgroud)\nProgram.cs:
System.Console.WriteLine("Hello, Console!");\nSystem.Windows.Forms.MessageBox.Show("Hello, Popup!");\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n\n笔记:
\nOutputType为.csproj而WinExe不是Exe。跨平台ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">\n <PropertyGroup>\n <OutputType>Exe</OutputType>\n <TargetFramework>net6.0</TargetFramework>\n <Configurations>Debug;Release;WindowsDebug;WindowsRelease</Configurations>\n </PropertyGroup>\n <PropertyGroup Condition="\'$(Configuration)\' == \'WindowsDebug\' Or \'$(Configuration)\' == \'WindowsRelease\'">\n <DefineConstants>WindowsRuntime</DefineConstants>\n <TargetFramework>net6.0-windows</TargetFramework>\n <UseWindowsForms>true</UseWindowsForms>\n </PropertyGroup>\n</Project>\nRun Code Online (Sandbox Code Playgroud)\n跨平台Program.cs
System.Console.WriteLine("Hello, Console!");\n\n#if WindowsRuntime\nSystem.Windows.Forms.MessageBox.Show("Hello, Popup!");\n#endif\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5439 次 |
| 最近记录: |