Injecting a .NET Object into JavaScript Using DotNetBrowser 1.x

Опубликовано: 24 Август 2017
на канале: Embedded Browsers
1,512
19

Today we are talking about Injecting a .NET Object into JavaScript Using DotNetBrowser
Get a free 30-days DotNetBrowser trial licence: https://www.teamdev.com/dotnetbrowser...

Previous tutorials:

How to get DotNetBrowser Evaluation Licence -    • How to get DotNetBrowser Evaluation L...  

How to Add a Browser Component to WPF Application -    • How to Add a Browser Component to WPF...  

How to Create HTML UI for WPF Application Using DotNetBrowser -    • How to Create HTML UI for WPF Applica...  

How to Add a Browser Component to a Windows Forms Application -    • How to Add a Browser Component to a W...  

Executing JavaScript from the .NET Side Using DotNetBrowser -    • Executing JavaScript from the .NET Si...  

About DotNetBrowser:
DotNetBrowser allows embedding a Chromium-based browser to your .NET app to display and process HTML5, CSS3, JavaScript, Flash etc.

For more ideas on using an advanced browser-component in your .NET application, please see examples on DotNetBrowser support site https://dotnetbrowser.support.teamdev...


Transсript:

00:07 Create and Configure a WPF Solution with DotNetBrowser

00:12 Create a WPF project. Then add the DotNetBrowser NuGet package and the valid licenсe file as an embedded resource

00:22 You can find detailed instructions in the previous tutorials. (Check out the links under this video)

00:24 Mark up the MainWindow

00:26 Add the Xml Namespace for the DotNetBrowser.Wpf library in the MainWindow.xaml

00:36 Create the WPFBrowserView component and define its name

00:42 Implement the MainWindow Behaviour

00:45 Define the public ‘DotNetObject’ class

00:49 Then define the public void ‘HandleMessage’ method with the ‘message’ argument

00:58 Inside this method begin invoking the ‘MessageBox.Show’ method using the CurrentDispatcher. The usage of the Current dispatcher is required because the JavaScript side calls injected objects from the non-UI thread

01:19 Now you need to create the ‘Browser.FinishLoadingFrameEvent’ handler

01:26 Inside the handler check that the loaded frame is the main frame

01:30 Then create the ‘JSValue jsWindow’ variable

01:35 And get the ‘window’ object from the JavaScript side using the ‘Browser.ExecuteJavascriptAndReturnValue’ method

01:42 Inject the new instance of the ‘DotNetObject’ class using the ‘JsObject.SetProperty’ method

01:55 After subscribing to the ‘FinishLoadingFrameEvent’ , call the ‘Browser.LoadURL’ method with the path to the html file which will be created in the next step

02:13 Create an HTML File

02:17 Add a new html file to the solution and call it “index.html”

02:23 Change the “Copy to Output Directory” property value to “copy always”

02:28 Add the text input with the “textbox” ID value

02:36 Add a button and call the ‘CallDotNet’ function in the onclick handler

02:45 Add the script section

02:53 Define the ‘CallDotNet’ function

02:56 Get the text from the ‘textbox’ element

03:07 And then call the ‘window.DotNetObject.HandleMessage()’ method

03:13 The name of the window property we’re calling corresponds to the name of the registered .NET object

03:18 Build and Launch


#.NET
#JavaScript
#DotNetBrowser
#EmbeddedBrowsers