none
WebBrowser Control cancel Navigating then InvokeScript RRS feed

  • Question

  • Hi,

    i don't know whether is this the right way. With the WebBrowser-Control on a Windows Phone 8 i have to open a website. If the login is required the site send a Redirect do an URI like http://appcontrol/login_required. This redirect should be cancelled in the Navigating event with e.Cancel = true.

    Now i have to call a JavaScript function on the first site with the credentials saved in the app-localstorage. Calling InvokeScript in the Navigating-Event i got a error message like this: "It is invalid to use this property or method during a Navigating event handler."

    Also without InvokeScript there is no other event after cancelling the Navigating.

    Does anyone have an idea or hint.

    Thanks for help

    Stephan

    void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("navigating "+e.Uri.ToString());
                var newHost = e.Uri.Host;
    
                if (newHost == "appcontrol")
                {
                    // ERROR !
                    webBrowser1.InvokeScript("login", "user", "password");
                    e.Cancel = true;
                }
            }
    


    C#-Beginner

    Thursday, September 12, 2013 6:46 PM

Answers

  • You can use Dispatcher.BeginInvoke(...) to queue the action for execution after the current code completes.

    ex:

    if (newHost == "appcontrol")
        {
        // ERROR !
        // set cancel outside of Dispatched action
        e.Cancel = true;
    
        // Dispatch InvokeScript action for later execution 
        Dispatcher.BeginInvoke(() => {
            webBrowser1.InvokeScript("login", "user", "password");
        });
    }
    


    -Eric.

    • Marked as answer by Stephan3 Monday, September 16, 2013 9:07 AM
    Thursday, September 12, 2013 8:33 PM