Thursday, 7 May 2015

Call C# function from Javascript in WPF

I was developing a desktop app in WPF and where I need to call C# function from Javascript.
I searched and found a post. I know there are so many posts are available on web but I am sharing this only to add one more resource so no one take time to solve their problem.
Very first, below XAML code.

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="GrdMain">

Now code behind,

public partial class MainWindow : Window
        // This nested class must be ComVisible for the JavaScript to be able to call it.
        public class ScriptManager
            // Variable to store the form of type Form1.
            private MainWindow ParantWindow;

            // Constructor.
            public ScriptManager(MainWindow form)
                // Save the form so it can be referenced later.
                ParantWindow = form;

            // This method can be called from JavaScript.
            public void MethodToCallFromScript()
                // Call a method on the form.

            // This method can also be called from JavaScript.
            public void AnotherMethod(string message)

        public void AlertMessage()
            // Indicate success.
            MessageBox.Show("Hey! worked.");

        public MainWindow()

            WebBrowser web = new WebBrowser();

            // Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#.
            web.ObjectForScripting = new ScriptManager(this);

            // Create the webpage.
            String HTMLText = @"<html>
                   <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                    <br />
                    <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello, How are you?');"" />

            web.NavigateToStream(new System.IO.MemoryStream(Encoding.ASCII.GetBytes(HTMLText)));

What happened here, There is no way to communicate between application and web browse object.
So to solve this problem I created one class which contains the object of our main window form. Now we’ll pass that class to web object and in web object our javascript resides.
Hope you like this post.

Enjoy coading…


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  2. Yesterday I came to visit my sister and we decided to have fun and play online casinos, polazili on a bunch of sites and this one caught my eye unforgettable good online casino Here we quickly figured out what was happening and began to think quickly about the strategy. and we found her now we take money out of here every day

  3. Any idea how to import a js file in wpf and call it on a html dom (not using WebBrowser).
    I want to Connect my wpf application to a link, get its html and perform a js operation.