处理Silverlight onerror回调

3
银光对象标签接受一个'onerror'参数,它调用一个你选择的javascript部分。Visual Studio模板生成的默认实现组装了一个描述性消息,并将其作为Error抛出(这样让IE显示小警告三角形)。
在我看来,每次触发此回调时,我们的Silverlight实例都已经死亡,需要刷新页面。这个推断合理吗?
另一个问题是如何最好地处理这个回调。显示小警告图标有些开发人员为中心,而且它不允许我们(开发人员)在客户机上运行时发现实际失败的原因。人们对此做了什么?我们自己(或多或少具体的)的一些想法包括:
- 通过某个公开的端点将错误消息发送回服务器 - 删除隐藏Silverlight对象,向用户显示更好和更描述性的消息以及'刷新'链接,重新启动Silverlight页面(我们运行全尺寸Silverlight应用程序,因此如果Silverlight对象不起作用,客户机可能需要重新加载) - 以某种方式自动重新加载对象标记,以避免客户执行任何操作以再次启动(可能与向客户发出“系统”已重新启动的通知相结合)
想法、思路、最佳实践、反模式?除了确保Silverlight应用程序永远不会失败之外,你们还在做什么?
2个回答

3

我喜欢使用SilverlightFX以“表单”形式展示优美的错误报告。你可以在http://github.com/NikhilK/silverlightfx/tree/master上获取源代码,这是一个非常酷的框架。基本上,它只是一个错误的摘要,能够通过上传发送电子邮件给支持人员,并带有“糟糕,我们出了点差错”的标题 :) 我会用这种方式处理所有未处理的异常,还有一篇关于使用字典按类型处理错误的好文章:http://www.netfxharmonics.com/2009/04/Exception-Handlers-for-Silverlight-20,也是我最喜欢的文章之一。希望这能帮到你。


一定要阅读这两个链接 - 但是你(Soren)也要阅读 App.xaml.cs 代码本身,以了解异常如何从 Silverlight 冒泡到 DOM/Javascript?特别是 Application_UnhandledExceptionReportErrorToDOM 的默认实现。Microsoft 提供的代码(和注释)可能也会给你一些想法。 - Conceptdev
将此标记为答案,因为有很好的链接,但一定要查看下面Eric Morks的答案,我喜欢他提到的可重启Silverlight内容。 - soren.enemaerke

2

我建议您更改实例化Silverlight控件的方式。不要使用object标记,而是在Silverlight.js文件中调用Silverlight.CreateObjectEx()。这对于您的情况可能更加自然。如果失败了,您可以再次调用它,这比尝试重新加载对象标记更简单。例如:

Silverlight.createObjectEx(
     {
         source: '/../VideoPlayer.xap',
         parentElement: document.getElementById('movieplayerDiv'),
         id: 'VideoPlayer',
         properties: {
             width: '100%',
             height: '100%',
             background: '#FFd9e3d7',
             inplaceInstallPrompt: false,
             isWindowless: 'false',
             version: '2.0'
         },
         events: {
             onError: onSLError,
             onLoad: onSLLoad
         },

         initParams: "...",
         context: null
     });



    function onSLLoad(sender, args) {

    }

    function onSLError(sender, args) {
        //                alert('error');
    }

其他想法:

  • If your javascript error handler fires, the instantiation has failed. Silverlight will not suddenly come back to life by itself. Instead, call CreateObjectEx to give it another shot ;)
  • There are 2 kinds of terminal errors in Silverlight. 1) Managed errors (hit the managed Application_UnhandledException method). Note that some errors may not even get to this point. If the managed infrastructure can't be loaded for some reason (out of memory error maybe...), you won't get this kind of error. Still, if you can get it, you can use a web service (or the CLOG project) to communicate it back to the server. 2) Javascript errors. These are what you're getting right now. I recommend bouncing these back to your server with JQuery (or Javascript library of preference). Something like:

        $.post(this.href, { func: "countVote" },
    

    function(data) {...} How you handle this on the server, of course, is dependent on what your server stack looks like. It's super easy in MVC, btw.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接