Skip to content

webview-cs/webview-cs

Repository files navigation

webview-cs

Build status

C# Bindings to https://github.com/zserge/webview. Easily create cross-platform desktop applications with web technologies.

Installation

You can get your hands on Webview.Core from NuGet:

PM> Install-Package Webview.Core

or from the dotnet command line:

$ dotnet add package Webview.Core

Examples

There are two main APIs to create a webview; the simple API, and the builder API. With the simple API all interaction takes place via the Webview.Webview.Simple method. This allows you to quickly get a webview running, but doesn't provide the ability to register an invoke callback for JS amongst other things. This is a good starting place.

using Webview

[STAThread]
public static void Main(string[] args)
{
    Webview.Webview.Simple("Window Title", "https://google.com"))    
}

You can also provide an initial window size and control if the window can be resized:

using Webview;
using System.Drawing;

Webview.Webview.Simple(
  "Title",
  Content.FromHtml("<html><h1>Hello World!"),
  size: new Size(500, 200),
  resizable: true
)

For the builder API you first need to create a WebviewBuilder. A fluent API allows you to choose how the Webview is created. Once built the resulting view must manually be Run to display the window.

using Webview;
using System.Drawing;

new WebviewBuilder(new Uri("http://google.com"))
    .WithSize(new Size(1024, 768))
    .Resizeable()
    .Debug()
    .WithInvokeCallback((webview, action) => {
      Console.WriteLine("Action: {0}", action);
    })
    .Build()
    .Run();

If you're program is going to outlive the Webview you can wrap it in a using statement to make sure unmanaged resources are disposed of when you expect them to be:

using Webview;

using (var webview = new WebviewBuider("Title", Content.FromHtml("<p>Hello World")
                        .WithCallback(MyCallbackFunction))
                        .Build())
{
    webview.Run();
}

ASP .NET Core WebHost

To create a standalone desktop application out of an ASP.NET Core website, use the Webview.WebHost nuget package.

PM> Install-Package Webview.WebHost

or from the dotnet command line:

$ dotnet add package Webview.WebHost

Replace the default Main function in Program.cs with this:

using Webview.WebHost;
...
    [STAThread]
    public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).WithDynamicPort().WithNoOutput().Build().RunWebview();
        }

To create the standalone executable, the project must be published.

$ dotnet publish -r win10-x64 -c Release

Supported runtime identifiers are 'win10-x64', 'win10-x64', 'osx-x64', and 'linux-x64'.

Windows Console Gotchas

Note - Windows has the concept of console and windows applications. For this reason, we need to attribute the main function:

[STAThread] // This is mandatory in Windows for the webview to be displayed.
static void Main(string[] args) {
    ...
}

Adding an Application Icon on Windows

To add an icon to your Windows application create a .NET Framework executable instead of a .NET Core one and attach an icon to that. See this issue for more information.

Feature Status

  • Run webview with standard parameters.
  • Builder API for creating webviews.
  • Native binaries for:
  • Linux x64
  • macOS x64
  • Windows10 x64
  • WebHostBuilder and WebHost extensions for running a .NET Core website as a desktop application