Git Product home page Git Product logo

webview_csharp's Introduction

ReleaseBuild WebviewNative Nuget Webview Org Chat

sharpWebview

This repository contains battery included C# bindings for zserge/webview. It only supports x64 systems.

Webview

zserge/webview is a small C/C++ header only library for a cross platform access of a webview control. It uses Edge Chromium, with fallback to the 'old' Edge, on Windows, GTK Webkit on Linux and Cocoa Webkit on macOS. sharpWebview ships precompiled libraries for each system, ready to be used in your next C# project. This repository contains a cmake file to compile webview via Github Actions WebviewNative.

You are always able to see which webview version sharpWebview uses by looking into the CMakeLists.txt (GIT_TAG option in the FetchContent_Declare command). You can find all compiled libraries and used patches in the libs folder of this repository.

All patches are also contributed back to zserge/webview.

REMARK: The nuget uses this fork at the moment, because the work on upstream is stalled at the moment. Because of the many patches it was not possible to maintain it without a fork.

Get started

A basic example

Create a new .net core console application and add the SharpWebview nuget. Use the dotnet command line or the package management in Visual Studio, if you use it.

dotnet add package SharpWebview

Always add the [STAThread] attribute to the main method. This is necessary to work on windows at least.

[STAThread]
static void Main(string[] args)

To create a webview use a using block. This way you make sure that the native webview gets disposed correctly!

using SharpWebview;

[...]

using(var webview = new Webview())
{
}

To open a basic webview which is pointing to a wikipedia article use the following code:

using(var webview = new Webview())
{
    webview
        .SetTitle("The Hitchhicker")             
        .SetSize(1024, 768, WebviewHint.None)
        .SetSize(800, 600, WebviewHint.Min)
        .Navigate(new UrlContent("https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy_(novel)"))
        .Run();
}

The examples folder contains two basic projects. The Minimal projects shows you a basic example on how to create a cross platform webview and how to open a URL in it. Please have a look into the documented Program.cs. You are also able to use the HtmlContent to provide some HTML which the webview will render.

Run a webserver to serve a javascript application

Besides the UrlContent and HtmlContent, sharpWebview also provides a HostedContent. This content type creates a webserver to which the webview will automatically navigate.

To use this content it is necessary to create a app folder in your project. Every file you put into this folder will be served by the HostedContent. The DesktopApp project is an example of the usage of this content type. Don't forget to set the files in the app folder to copy always (see project file for an example).

HostedContent on Windows systems

The Edge webview uses a UWP application context on windows. UWP applications disallow loopbacks. For development purpose it is necessary to run the following command in an administrative command prompt:

CheckNetIsolation.exe LoopbackExempt -a -n="Microsoft.Win32WebViewHost_cw5n1h2txyewy"

This adds the Edge Webview Host to the exception list of this limitation. Your best bet for application distribution is to create an installer which executes this command on installation.

Application Distribution

Windows

The DesktopApp example contains a simple script to create a MSI installer. You are able to take the wix.bat and DesktopApp.wix files as a starting point for your application. To use the wix.bat you need to install the WIX Toolset.

I highly recommend to use scoop to install it. Scoop is a command line installer for easy installation of many different applications. Just run

scoop install wixtoolset

to install WIX. After this you should be able to execute the wix.bat to create a basic installer for the example DesktopApp.

webview_csharp's People

Contributors

geaz 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.