Quick Start for WPF
In this article, you will learn how to download the DotNetBrowser library, get an evaluation license, and create, configure and run your first .NET WPF Application which loads and displays the DotNetBrowser Home Page.
Environment:
Windows 10, Microsoft Visual Studio 2017, .NET Framework 4.6.
The sample solution which demonstrates how to embed lightweight or heavyweight controls into WPF applications can be downloaded here. The projects in this solution have NuGet dependencies which will be resolved automatically during build.
1. Download the library
To download DotNetBrowser library, navigate to http://www.teamdev.com/dotnetbrowser and click the Download button. Extract the downloaded dotnetbrowser-2.x.zip archive into the D:\Projects\DotNetBrowser\
directory.
2. Get license
To get a free 30-day evaluation license, fill the web form and click the GET EVALUATION button. You will receive an email with the license key that you should use to run the example.
3. Create a WPF application
Create a new WPF.DotNetBrowser
WPF Application C# Project or WPF Application Visual Basic Project in the D:\Projects\DotNetBrowser
directory:
4. Add DotNetBrowser DLL assemblies
In the Solution Explorer, right-click References and select the Add References… menu item:
In the Reference Manager which opens, click the Browse button:
Select the DotNetBrowser.dll
, DotNetBrowser.Core.dll
, DotNetBrowser.Logging.dll
, DotNetBrowser.Wpf.dll
files and click Add:
Double-check all selected references are added and appear in the Reference Manager dialog and click OK:
DotNetBrowser.ChromiumXX.dll
is found automatically if it is located in the same folder as DotNetBrowser.dll
. You can also add it to your references, if the project is deployed using Microsoft Visual Studio. Both assemblies are required.
5. Add a license
To embed the license key into your project, copy the license key string from the email and insert it as shown below:
IEngine engine = EngineFactory.Create(
new EngineOptions.Builder
{
LicenseKey = "your_licese_key"
}
.Build());
For more information on license installation, refer to this article.
6. Change the source code
Insert the following code into the MainWindow.xaml file:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:WPF="clr-namespace:DotNetBrowser.Wpf;assembly=DotNetBrowser.Wpf"
x:Class="DotNetBrowserSample.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="480" Width="800" Closed="Window_Closed">
<Grid>
<WPF:BrowserView Name="browserView" />
</Grid>
</Window>
Use the code sample below for the MainWindow.xaml.cs file:
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using DotNetBrowser.Browser;
using DotNetBrowser.ContextMenu;
using DotNetBrowser.Engine;
using DotNetBrowser.Handlers;
namespace DotNetBrowserSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IBrowser browser;
private IEngine engine;
#region Constructors
public MainWindow()
{
Task.Run(() =>
{
engine = EngineFactory.Create(new EngineOptions.Builder
{
RenderingMode = RenderingMode.HardwareAccelerated,
LicenseKey = "your_license_key_goes_here"
}
.Build());
browser = engine.CreateBrowser();
})
.ContinueWith(t =>
{
browserView.InitializeFrom(browser);
browser.Navigation.LoadUrl("https://teamdev.com/dotnetbrowser");
}, TaskScheduler.FromCurrentSynchronizationContext());
InitializeComponent();
}
#endregion
#region Methods
private void Window_Closed(object sender, EventArgs e)
{
browser.Dispose();
engine.Dispose();
}
#endregion
}
}
7. Run the application
To run the application, press F5 or click the Start button on the toolbar. The MainWindow opens: