【问题标题】:Silverlight app closing one window closes the parent window alsoSilverlight 应用程序关闭一个窗口也会关闭父窗口
【发布时间】:2011-08-10 20:37:17
【问题描述】:

在我们的 Silverlight 应用程序中,我们打开了一个托管在新 HTML 页面中的新 Silverlight 控件,只要用户在二级页面加载之前没有点击浏览器关闭按钮,一切都会很好。然后两个浏览器窗口都消失了,没有任何警告。我尝试编写 Javascript 来处理 onbeforeunload,但即使这样也没有被调用。这个想法是警告用户这个操作可能是坏的。因此,我编写了一个真正简单的示例应用程序来重现该问题,这里是 ni-lites:

向父页面添加一个按钮:

 HtmlPage.Window.Navigate(new Uri(String.Format("TestControlTestPage.aspx"),
                UriKind.Relative), "searchresults", 
                "directories=no,location=no,menubar=no,status=yes,toolbar=no,resizable=yes");

这将打开一个新的 HTML 页面,其中包含一个新的 Silverlight 控件 newControl,在这里我模拟了我们的应用程序中必须从服务器加载一些内容的情况,如下所示:

public MainPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);
            System.Threading.Thread.Sleep(10000); 
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

所以在调用加载的方法之前会有延迟,如果我在调用加载的页面之前按 X(关闭),那么两个窗口都会消失。

如上所述,我也尝试为 onbeforeunload 添加 javascript,但没有调用。非常丑陋的任何想法将不胜感激

【问题讨论】:

  • 我也有这个问题,有没有人提出可能的原因或解决方案?

标签: windows silverlight


【解决方案1】:

试试我的解决方案Stopping a user from leaving a Silverlight page

我不知道为什么你之前使用 onbeforeunload 失败了,因为它确实很好用,但也许你发布了那个 javascript 代码,我们可以解决这个问题。

【讨论】:

    【解决方案2】:
    <script type="text/javascript">
        alert("askconfirm function set");  //make sure it is loaded
    adjustSilverlightHeight();
        window.onbeforeunload = function () {
            alert("askconfirm");   //confirm it is called
            var control = document.getElementById("SilverlightControl");
    
            if (control.Content.Page) {
                var IsLoading = control.Content.Page.IsLoading();
    
                if (!IsLoading) {
                    return;
                }
    
                return 'Closing this window before it loads can cause application instability , Please click  Stay on Page option';
            }
        }
    </script>
    

    【讨论】:

    • 在尝试使用 Javascript 时出现 JS 错误:“Content.Page 为空或不是对象”。当我添加一个测试 Content.Page 确实为空时,这是一个运行时错误,使您的代码停止工作。尝试我的博客链接中的确切示例。干杯
    • 问题是如果您通过javascript加载silverlight控件,页面可能还没有完全加载。使用您的示例,您从 JS 调用一个 silverlight 方法,但如果它没有加载,这有什么用?无论哪种方式,onbeforeunloaded 在这种情况下都没有帮助,即使是通过纯 JS,因为时间使它不可靠。
    猜你喜欢
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多