2
votes

in our Silverlight app we open up a new Silverlight control hosted in a new HTML page and things work great as long as the user does not hit the browser close button before the secondary page loads. Then both browser windows disappear with no warning. I tried writing Javascript to handle the onbeforeunload but even this was not called. The idea was to warn the user that this action could be bad. So I then write a real simple sample app to repro the issue and here are the ni-lites:

add a button to the parent page:

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

This opens a new HTML Page hosting a new Silverlight control , newControl and in here I simulate the situation in our app where we must load some stuff from the server as follows:

public MainPage()
        {
            InitializeComponent();

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

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

So there is delay before the loaded method is called, and if I press the X (Close) before the page loaded is called then both windows disappear.

as mentioned above I also have a tried adding javascript for the onbeforeunload but this was not called. Pretty ugly any ideas would be appreciated

2
I am also having this issue, has anyone come up with a possible cause or solution?ginman

2 Answers

0
votes

Try my solution for Stopping a user from leaving a Silverlight page

I am not sure why your previous use of onbeforeunload failed, as it works really well, but perhaps you post that javascript code and we can work out that issue.

0
votes
<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>