1

我的 Silverlight 有时需要打开一个新的浏览器窗口。这不是问题。

但是我在对关闭的打开窗口做出反应时遇到问题(我需要刷新数据)。现在,如果不是因为我打开的新页面可以(并且确实)执行回调,这不会成为问题。因此,onbeforeunload事件并没有那么有用,因为它不仅会在窗口关闭时触发,还会在页面执行回调时触发。

这是我目前得到的代码。

    private void OpenAnnotation()
    {
        var window = HtmlPage.Window.Navigate(new Uri(/*url goes here*/)),
            "_newWindow", "toolbar=0,menubar=0,resizable=1,scrollbars=1");
        SetWindowCallback(window, TimeSpan.FromSeconds(0.5)); //Delay needed, the OS needs at least some time to display the new window.
    }

    private void SetWindowCallback(HtmlWindow window, TimeSpan delay)
    {
        System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            System.Threading.Thread.Sleep(delay);
            UIDispatcher.Invoke(() =>
            {
                if (window != null && Helpers.HtmlWindow.WindowExists(window)) //WindowExists attemts to do Invoke("this") on the HtmlWindow; if it fails, it's likely the window doesn't exist
                {
                    window.Eval(@"this.attachEvent('onbeforeunload', 
function(){
    var slObject = window.opener.document.getElementById('SL');
    if (slObject != null)
        slObject.Content.AnnotationControl.UpdateAttachmentInfo(this);
});");
                }
            });
        });
    }


    [ScriptableMember]
    public void UpdateAttachmentInfo(HtmlWindow window)
    {
        SetBusyIndicator(true);
        System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));
            LoadAnnotation();
            SetWindowCallback(window, TimeSpan.Zero);
        });
    }

我真的“只是”想在窗口关闭时刷新我的数据,但似乎我仅限于 JS,而这反过来又会对提到的回调做出反应。

有没有更好的方法来做到这一点?

PS。我不能确定我正在打开的页面的代码!我只能使用我的 SL 应用程序。

4

0 回答 0