8

.Net 5的控制台程序中使用桌面类库

 4 years ago
source link: https://www.cnblogs.com/TianFang/p/14090148.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

今天试了一下在.net 5控制台程序中使用桌面类库, 发现遇到了一些障碍。虽然在.net 5中引用桌面库比较简单,只要在csproj文件中将TargetFramework改为"net5.0-windows",并且把UseWindowsForms设置为"true"即可。

    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

 执行一个简单的示例:

    static void Main(string[] args)
    {
        MessageBox.Show("hello world");
        Console.WriteLine("Hello World!");
    }

运行的时候却发现,虽然对话框能很好的显示出来,但是却没有控制台窗口。将TargetFramework或者UseWindowsForms改回去后,就编译报错,不支持引用桌面类库。最后,网上搜了一下,找到了如下解决方案: 在csproj文件中,添加DisableWinExeOutputInference并设置为"true"即可。

    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <DisableWinExeOutputInference>true</DisableWinExeOutputInference>

 此外,我还发现了一个间接的方法:把UseWindowsForms设置为"false",引用别的工程,通过调用别的程序集中的方法一样可以在显示控制台窗口的方式下,调用桌面类库。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK