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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="GrdMain">
       
    </Grid>
</Window>


Now code behind,

public partial class MainWindow : Window
    {
        // This nested class must be ComVisible for the JavaScript to be able to call it.
        [System.Runtime.InteropServices.ComVisible(true)]
        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.
                ParantWindow.AlertMessage();
            }

            // This method can also be called from JavaScript.
            public void AnotherMethod(string message)
            {
                MessageBox.Show(message);
            }
        }

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

        public MainWindow()
        {
            InitializeComponent();

            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>
                <head>
                       <title>Test</title>
                </head>
                <body>
                   <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                    <br />
                    <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello, How are you?');"" />
                </body>
                </html>";

            web.NavigateToStream(new System.IO.MemoryStream(Encoding.ASCII.GetBytes(HTMLText)));
            GrdMain.Children.Add(web);
        }
    }


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…
Thanks.

No comments:

Post a Comment