How to debug JavaScript code of a page from WPF WebBrowser Control

I have a WPF WebBrowser control that I used in my wpf client application to navigate to different pages. It has basic functionality like back, forward, go events which calls this.webbrowser.goback (), this.webbrowser.goforward (), this.webbrowser.go ().

Even though the web app works fine with IE, which means all forward and backward navigation systems are more than one level. More than one-level navigation does not work in my WPF application on one page (Billings, which has a contact link and in the contact page we have an address link.) When I click the back button on my address page, it goes to the contact page. but the back button on the contact page does not go to the billing page.

Since it works fine in IE without any problem, I suspect something is wrong with my WPF applications. I have several questions here.

  • Is webbrowser.goback () the same as IE's back button? Or will any events be missed in the wpf web browser control versus IE?
  • Is there a way to debug javasctipts from within the WPF application itself?
  • Is there a better way to debug these scripts from WPF applications?
  • Does IE keep WPF WebBrowser navigation files in its history or is it a separate instance?

my control looks like below

<WebBrowser x:Name="webBrowser" DockPanel.Dock="Bottom" Navigating="webBrowser_Navigating" Navigated="webBrowser_Navigated" LoadCompleted="webBrowser_LoadCompleted"/>

      

And the code looks like this:

private void backButton_Click(object sender, RoutedEventArgs e)
    {
        // Navigate to the previous HTML document, if there is one
        if (this.webBrowser.CanGoBack)
        {
            this.webBrowser.GoBack();
        }
        else
        {
            MessageBox.Show("Cannot go back. There needs to be something in the history to go back to.");
        }
    }
    private void forwardButton_Click(object sender, RoutedEventArgs e)
    {
        // Navigate to the next HTML document, if there is one
        if (this.webBrowser.CanGoForward)
        {
            this.webBrowser.GoForward();
        }
        else
        {
            MessageBox.Show("Cannot go Forward. There needs to be something in the history to go forward to.");
        }
    }

      

+3


source to share


2 answers


I would expect the WebBrowser control navigation to work the same as IE hosted in WPF.

You can have JavaScript calling methods in a .NET application, so it can have JavaScript debugging information in the .NET console.

This class can be passed to JavaScript to WebBrowser to call a .NET method:

[ComVisible(true)]
public class ScriptManager
{
    // This method can be called from JavaScript.
    public void WriteConsole(string output)
    {
        // Call a method on the form.
        Console.WriteLine(output);
    }
}

      

Go to WebBrowser:



webBrowser1.ObjectForScripting = new ScriptManager();

      

I don't know what bit of JavaScript to call the method as I have never used JavaScript, but I think it would be something like this:

$scope.LogBackNavigation = function() {
    window.external.WriteConsole("Back Navigation");
}

      

This can let you see what's going on in your JavaScript.

+3


source


Debugging - Rollback to Process -> Script Code This loaded all the JS files in my VS and I could put a breakpoint wherever I wanted.



+1


source







All Articles