Git Product home page Git Product logo

webview-cs's Introduction

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

webview-cs's People

Contributors

iwillspeak avatar zenakuten avatar frassle avatar ghaarg avatar jacob-l avatar konh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.