Neeraj Goel
Neeraj Goel

Reputation: 282

How to send data from C# to webview HTML file in Xamarin

I am developing a project using Xamarin.forms. I need to get data from webservice and then need to show that data on WebView HTML page. Suggest me any Xamarin.forms inbuild function or any renderer class to implement this feature.

Upvotes: 0

Views: 2541

Answers (2)

Neeraj Goel
Neeraj Goel

Reputation: 282

To send data from C# page to HTML we need to call javascript function defined in that HTML file. We are having Eval function in Xamarin Forms.

WebViewObj.Eval(string.Format("JSFunctionName({0})", DataInStringFormat));

By using above function we can send the data to JavaScript function.

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34083

You probably want to look at the documentation for creating a HybridWebView.

This document describes how you can create your own kind of WebView which can handle JavaScript events for you. Although it may not be 100% accurate to what you want, it will give you an idea and you can pick it up from there.

As you noted in the comment you need to implement C# to JS. In the same custom renderers you can call functions for this on the WebViews. For iOS you can use InvokeJavaScript(); and for Android (mis)use LoadUrl("javascript: alert('Hi!');").

For more info on this, follow this great post by Adam Pedley.

Upvotes: 0

Related Questions