1

我使用以下代码显示OpenFileDialog使用RoslynPad,它编译并运行,但没有出现对话框,因此代码段将永远运行:

#r "framework:Microsoft.WindowsDesktop.App"

using System.Windows.Forms;

var fd = new OpenFileDialog
{
    Filter = "Solution files (*.sln)|*.sln"
};

if (fd.ShowDialog() == DialogResult.OK)
    Console.WriteLine(fd.FileName);

OpenFileDialog使用 RoslynPad的正确方法是什么?

环境:

  • 操作系统:Windows 10 Pro 64 位 (2004)
  • RoslynPad:由最新的 master 分支构建。
  • .NET 核心:3.1.402
4

1 回答 1

1

检查回购后,我可以OpenFileDialog通过添加以下行来完成工作:

await Helpers.RunWpfAsync();

完整代码如下:

#r "framework:Microsoft.WindowsDesktop.App"

using Microsoft.Win32;

await Helpers.RunWpfAsync(); // initializes a dispatcher thread

var fd = new OpenFileDialog
{
    Filter = "Solution files (*.sln)|*.sln"
};

if (fd.ShowDialog() == true)
    Console.WriteLine(fd.FileName);

不确定这是否是最好的方法,但它有效!

于 2020-09-28T11:15:39.313 回答