I am developing a cross platform xamarin forms application. I have created a Portable Cross-Platform App, in which I am trying to open local HTML pages in webview. For calling C# function from Angularjs, I have implemented hybrid web view concepts.
My Angularjs Function
$scope.SyncServerJobs = function () {
//$scope.loadActList();
window.CommInterface.syncServerJobs("");
}
In My Universal Windows Project
public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Windows.UI.Xaml.Controls.WebView>
{
public CommInterface communicator = new CommInterface();
const string JavaScriptFunction = "function invokeCSharpAction(data){window.external.notify(data);}";
const string SyncServerJobs = "function syncServerJobs(data){window.external.notify(data);}";
protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
var webView = new WebView();
webView.Settings.IsJavaScriptEnabled = true;
SetNativeControl(webView);
}
if (e.OldElement != null)
{
Control.NavigationStarting -= OnWebViewNavigationStarted;
Control.NavigationCompleted -= OnWebViewNavigationCompleted;
Control.ScriptNotify -= OnWebViewScriptNotify;
}
if (e.NewElement != null)
{
Control.NavigationStarting += OnWebViewNavigationStarted;
Control.NavigationCompleted += OnWebViewNavigationCompleted;
Control.ScriptNotify += OnWebViewScriptNotify;
Control.Source = new Uri(string.Format("ms-appx-web:///Content//{0}", Element.Uri));
}
}
private void OnWebViewNavigationStarted(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (Control != null && Element != null)
{
communicator = new CommInterface();
Control.AddWebAllowedObject("CommInterface", communicator);
}
}
async void OnWebViewNavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (args.IsSuccess)
{
// Inject JS script
await Control.InvokeScriptAsync("eval", new[] { JavaScriptFunction });
await Control.InvokeScriptAsync("eval", new[] { SyncServerJobs });
}
}
void OnWebViewScriptNotify(object sender, NotifyEventArgs e)
{
Element.InvokeAction(e.Value);
}
}
My commInterface class
[AllowForWeb]
public sealed class CommInterface
{
readonly WeakReference<HybridWebViewRenderer> hybridWebViewRenderer;
public CommInterface()
{
}
public void syncServerJobs(string data)
{
HybridWebViewRenderer hybridRenderer;
if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
{
hybridRenderer.Element.SyncServerJobs(data);
}
}
}
As like in Android project I am using CommInterface class to communicate with database class in Portable library. But in my Universal Windows project, I am not able to communicate with Csharp functions to run CSharp CRUD functions. How can I call the CSharp function from script?