HybridBridge
This is a PCL library that let you connect JavaScript on the browser side to the C# side by proxying C# code.
Install / Use
/learn @falahati/HybridBridgeREADME
Hybrid Bridge
This is a PCL library that lets you connect JavaScript on the browser side to the C# code by proxying C# side.
Table of Content
- WHERE TO FIND
- HOW IT WORKS
- HOW TO USE
- BRIDGE CONTROLLER (HybridBridge.BridgeController)
- CLASS BRIDGE (HybridBridge.ClassBridge<T>)
- HybridBridge.HybridMessagingHandler
- LIMITATIONS
- DOCUMENTATION
- LICENSE
How to get
This library is available as a NuGet package at nuget.org.
For this library to be usable you need to use one of the following platform packages.
Help me fund my own Death Star
--OR--
You can always donate your time by contributing to the project or by introducing it to others.
How it works
Unlike the majority of other libraries out there, HybridBridge put the focus on the JavaScript side instead of C# code. Using HybridBridge you can proxy your C# code on the JavaScript side and as result of this, you have full access to the C# class properties, methods, fields, and events.
Starting point of this library is the HybridBridge.BridgeController class which is a simple list of HybridBridge.IBridgeHandler implemented classes. Each HybridBridge.IBridgeHandler registered to this class will have the chance to response to the JavaScript side and this makes it easy to expand this library by simply writing new classes implementing HybridBridge.IBridgeHandler interface.
We also defined and shipped three class implementing HybridBridge.IBridgeHandler interface in this library, one for proxying C# classes (HybridBridge.ClassBridge), one for proxying C# enums (HybridBridge.EnumBridge) and another one just for two-way communication (HybridBridge.HybridMessagingHandler). In most cases, these are enough for your project and there is no need to write a new one.
On the other side, HybridBridge.BridgeController needs to be connected to the native Web Browser controls on each platform. This is done by inheriting this class in platform projects and packages. So you can share your JavaScript, C# code and Html files across all platforms and leave it to the platform packages to make the connection between native Web Browser control and the HybridBridge.BridgeController class.
This makes it possible to have a two-way communication between C# side and the browser (JavaScript) without any change to the logic of your program.
Following flowchart, tries to show the relationships between classes and the way communication and content delivery works in a cross-platform application using HybridBridge:

How to use
After adding the correct package to your project, it is possible to map the native Web Browser control to the HybridBridge class using the platform specific children of the HybridBridge.BridgeController class. Following examples demonstrate how to create a new instance of this classes and pass the native Web Browser control to be bridged.
Android
HybridBridge.Android package contains a class named WebViewHybridBridge inheriting from HybridBridge.BridgeController. Using this class, you are able to connect the WebKit.WebView control to the HybridBridge library.
var hybridBridge = new HybridBridge.Android.WebViewHybridBridge(webView);
iOS
HybridBridge.iOS package contains a class named UIWebViewHybridBridge inheriting from HybridBridge.BridgeController. Using this class, you are able to connect the Foundation.UIWebView control to the HybridBridge library.
var hybridBridge = new HybridBridge.iOS.UIWebViewHybridBridge(webView);
Universal Windows 10 Store Application (Windows 10 and Windows Phone 10)
HybridBridge.UWP package contains a class named WebViewHybridBridge inheriting from HybridBridge.BridgeController. Using this class you are able to connect the UI.Core.WebView control to the HybridBridge library.
var hybridBridge = new HybridBridge.UWP.WebViewHybridBridge(webView);
Unfortunately, because of limitations forced on us by the WebView control, WebViewHybridBridge cannot offer the full functionalities of this library. For more information about this please read the Windows Store and Windows Phone WebView limitations part.
As a workaround, however, HybridBridge.UWP offers another way of connecting the HybridBridge library to the WebView, enabling it to work in full capacity; WebViewHybridServer class is responsible for enabling full two-way communication in this situations by opening a local port and handling HTTP request directly.
However, for being able to use this class, you should add the Internet Server or Private Server capacity to your application's manifest file.
var hybridBridge = new HybridBridge.UWP.WebViewHybridServer(webView);
Universal Windows 8.1 Store Application and Windows Phone 8.1 Applications
HybridBridge.Win81 package contains a class named WebViewHybridBridge inheriting from HybridBridge.BridgeController. Using this class you are able to connect the UI.Core.WebView control to the HybridBridge library.
var hybridBridge = new HybridBridge.Win81.WebViewHybridBridge(webView);
Unfortunately, because of limitations forced on us by the WebView control, WebViewHybridBridge cannot offer the full functionalities of this library. For more information about this please read the Windows Store and Windows Phone WebView limitations part.
As a workaround, however, HybridBridge.Win81 offers another way of connecting the HybridBridge library to the WebView, enabling it to works in full capacity; WebViewHybridServer class is responsible for enabling full two-way communication in this situations by opening a local port and handling HTTP request directly.
However, for being able to use this class, you should add the Internet Server or Private Server capacity to your application's manifest file.
var hybridBridge = new HybridBridge.Win81.WebViewHybridServer(webView);
Windows Classic Applications (Windows Forms and Windows Presentation Foundation, WPF)
HybridBridge.Win package contains two class inheriting from HybridBridge.BridgeController making this possible to connect the Windows.Forms.WebBrowser and Windows.Controls.WebBrowser controls to the HybridBridge library.
However, this is important to clarify that none of these controls are natively compatible with HybridBridge mechanism for handling the request and as result of this, both classes defined in this package are working by opening a local port and listening for incoming HTTP request. This may opens a Security Hole to your application while running under Windows.
Also, you should take care of permissions required for opening a local port on the running system.
Windows Forms (WinForm)
WebBrowserHybridServer class is responsible for making the connection to the Windows.Forms.WebBrowser control:
var hybridBridge = new HybridBridge.Win.WebBr
