gpt4 book ai didi

c# - CrossThreadMessagingException WPF OpenFileDialog

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:20 25 4
gpt4 key购买 nike

我得到这个异常:

Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException was unhandled HResult=-2146232832 Message=An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred Source=Microsoft.VisualStudio.Debugger.Runtime StackTrace: at Microsoft.VisualStudio.Debugger.Runtime.Main.ThrowCrossThreadMessageException(String formatString) InnerException:

当我使用这段代码时:

    public string ShowOpenFileDialog(string initialPath, string filter = null)
{
var dialog = new OpenFileDialog()
{
InitialDirectory = Directory.Exists(initialPath) ? Path.GetDirectoryName(initialPath) : String.Empty,
FileName = File.Exists(initialPath) ? Path.GetFileName(initialPath) : String.Empty,
Filter = filter
};

if (dialog.ShowDialog() == true)
{
return dialog.FileName;
}

return String.Empty;
}

我没有明确使用多线程,那么是什么原因造成的呢?它在调试期间经常发生,但不是一直发生。我已经将方法签名更改了大约一百万次,因为我正在传递 ref/out 参数并认为它们可能是原因。

编辑:我从 UI 线程调用它。 View 中的按钮 -> ViewModel 中的命令 -> ShowOpenFileDialog。

最佳答案

虽然我仍然不确定为什么会发生这种情况,但我已经想出如何解决我的问题。我的解决方案中有多个项目,并且配置了两个启动项目(客户端+服务器)。当我通过调试启动两个项目时,调试器似乎有问题。所以我只是关闭了目前最不重要的项目的调试,因此调试器只处理一个项目。

关于c# - CrossThreadMessagingException WPF OpenFileDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com