Silverlight 3 XamlReader 异常未捕获

发布于 2024-08-11 07:00:33 字数 418 浏览 6 评论 0原文

当我将 XamlReader.Load() 与无效的 XAML 字符串一起使用时,虽然处于 try-catch-block 中,但不会捕获生成的 XAMLParseException:

 try
 {
        UIElement xamlCode = XamlReader.Load(XamlText) as UIElement;
 }
 catch (Exception ex)
 {
        ErrorText = ex.Message;
 }

该代码是从 DispatcherTimer 的 Tick-Event 调用的,但也在诸如 MouseLeftButtonDown 之类的事件中调用未捕获异常,导致我调用 .Load() 的行中断。

有谁知道如何捕获此异常并恢复正常的程序活动?

谢谢,安德烈

when I use XamlReader.Load() with an invalid XAML string, the resulting XAMLParseException is not caught although beeing in a try-catch-block:

 try
 {
        UIElement xamlCode = XamlReader.Load(XamlText) as UIElement;
 }
 catch (Exception ex)
 {
        ErrorText = ex.Message;
 }

The code is called from the Tick-Event of a DispatcherTimer, but also in Events like MouseLeftButtonDown the exception is not caught resulting in a break in the Line where I call .Load().

Does anyone know how to catch this Exception and resume normal programm activity?

Thanks, Andrej

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-08-18 07:00:33

这段代码不会捕获异常,这是完全不可思议的。如何确定此处发生了 XAMLParseException?您确定不是来自项目中的其他 Xaml Load?

It is completely unfathomable that this code would not catch the exception. How do you determine that the XAMLParseException is occuring here? Are you sure is not coming from some other Xaml Load in the project?

南城旧梦 2024-08-18 07:00:33

总是这样吗?还是仅在调试时?

我知道这是一个非常晚的答案,您可能已经找到了解决方案,作为参考,人们发现您的问题与他们的问题类似(例如我的情况),我的答案可能仍然有用。

如果在 debuggin 时发生,可能是因为异常被配置为抛出。

您可以更改此设置:

  1. 自定义“调试”菜单,向其中添加“异常”命令。
  2. 在异常配置中,深入到 System.Windows.Markup.XamlParseException,它位于公共语言运行时异常下。
  3. 删除“抛出”列中的勾选。

Is this always the case ? or onlys while debugging ?

I'm aware this is an extremely late answer and you might have found the solution to it, for as reference to people finding your question similar to theirse (like my case ), my answer might still be of use.

If its happening while debuggin, it might be because the exeption is configured to be thrown.

You can change this:

  1. Customize the Debug menu, adding the "Exceptions" command to it.
  2. In the Exceptions configuration, Drill down to System.Windows.Markup.XamlParseException, which is under Common Language Runtime Exceptions.
  3. Remove the check from the "Throw" column.
彼岸花ソ最美的依靠 2024-08-18 07:00:33

有多种 Silverlight 操作被“重新编组”到单独的线程上,大概有各种充分且充分的原因。它看起来有点像这样:

Dispatcher.BeginInvoke(() => LoadSomeXamlOrSomething());

LoadSomeXamlOrSomething() 中引发的任何异常都不会被普通的 try/catch 块捕获。即使在 SL 4 中,当加载格式无效的图像时也会发生这种情况。这很烦人,微软需要想出一个更好的方法来处理这个问题,例如,让你在调用时注册一个异常处理程序。

在 MS 弄清楚这一点之前,您的选择是:

  • 修复底层 XAML 错误。
  • 捕获 App.Application_UnhandledException 中的异常。

There are various Silverlight operations that get "re-marshalled" onto separate threads for what are presumably various good and sufficient reasons. It looks kind of like this:

Dispatcher.BeginInvoke(() => LoadSomeXamlOrSomething());

Any exception thrown within LoadSomeXamlOrSomething() won't be caught by normal try/catch blocks. This happens even in SL 4 with things like loading images with invalid formats. It's annoying, and MS needs to come up with a better way to handle this, for instance, by letting you register an exception handler when you make the call.

Until MS figures this out, your options are:

  • Fix the underlying XAML error.
  • Catch the exception in App.Application_UnhandledException.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文