wpf 在 PresentationFramework.dll 中发生 System.Reflection.TargetInvocationException'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16035170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll
提问by Naren
I run a simple C# WPF applicationthat uses a browser embedded in it. I have created a event when the browser navigates to the given url. But this bug comes during runtime:
我运行了一个简单的C# WPF application使用嵌入在其中的浏览器。当浏览器导航到给定的 url 时,我创建了一个事件。但是这个错误是在运行时出现的:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll
PresentationFramework.dll 中发生类型为“System.Reflection.TargetInvocationException”的未处理异常
Additional information: Exception has been thrown by the target of an invocation. The code is as follows,
附加信息:调用的目标已抛出异常。代码如下,
public MainWindow()
{
// Some initialisation
webBrowser.Navigate(requiredUrl);
}
private void webBrowser_Navigated(object sender, NavigationEventArgs e)
{
// Actions related to navigation
}
Its throwing the Exception.
它的投掷Exception。
采纳答案by Naren
Nevermind,the problem is solved..
没关系,问题解决了。。
Move the webBrowser.Navigate(requiredUrl) to Windows_Loaded event.
将 webBrowser.Navigate(requiredUrl) 移动到 Windows_Loaded 事件。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Some code
webBrowser.Navigate(requiredUrl);
}
It's working fine now.
它现在工作正常。
I think as Andy said the window needs a valid handle for the browser .
我认为正如安迪所说,窗口需要浏览器的有效句柄。
So it not working inside the MainWindow() function,as the handle for browser is not created in it.
所以它在 MainWindow() 函数中不起作用,因为浏览器的句柄没有在其中创建。

