Let’s build a communication bridge into your Xamarin.Forms WebView, and talk to it!? 😉 lol Yes let me show you how to pass data into the Web page rendered inside your Xamarin.Forms WebView!
WebView in Xamarin.Forms..
Xamarin.Forms provides a neat WebView that could render any Web HTML content efficiently similar to a browser inside your own Xamarin.Forms App.
Let’s give it a try and establish a uni-directional communication with our WebView! 😉
Let the coding begin!
Full code on github: /XFWebViewInteropDemo/DefaultWebViewDemoPage.xaml.cs
Alright, let’s try it out!
Well if you’re on Visual Studio, just hit F5 and watch the magic!
Side by side iOS, Android and UWP with Xamarin.Forms right out of the box! 😉
I have shared the demo app code in my github as usual: github.com/XFWebViewInteropDemo
No hacks, no work arounds, no custom renders, just straight out of the box in Xamarin.Forms! Works like a charm! 😉
Yep it’s that time, for some extra tips based on my experience with Xamarin.Forms WebView!
-Track Navigation inside the WebView
WebView provides a whole list of events to track the navigation now, along side back and forward navigation, redirects, and even refresh events.
This event Triggers upon the beginning of the Navigation, allowing you to cancel it on demand. This event provides a WebNavigatingEventArgs object which provides you all the details about the navigation that is about to occur, direction, url endpoint and so on.
This also provides WebNavigatingEventArgs.Cancel property which allows you to cancel that navigation on demand. So yeah a lot of cool bits you can do with it!
This event Triggers after a Navigation completes, providing you with the same details similar to Navigating event. In addition it gives WebNavigatedEventArgs.Result property which tells you whether the navigation was success or failure.
WebView.GoBackRequested | GoForwardRequested | ReloadRequested
Now these are some simplified events thats provided by WebView, allowing you to directly hook into GoBack, GoForward and Reload events when they occur. Although they do not provide facility to cancel those events like how we get in Navigating event. Just a quick easy way to monitor those events as they occur.
- Get device native data
- Location GPS data
- Proximity data
- Internet Connectivity data
- Captured File/Image data
Those are few examples, yes even an Image captured from the device camera can easily be sent as a byte array converted into a base64 string! Imagination is the limit yo! 😉
Xamarin.Forms WebView has come a long way since the early days, into a complete mature sandbox environment to render any HTML Web Content inside your Xamarin.Forms app. And it provides lots of features to communicate, pass data back and forth, and even monitor and control the navigation happens inside itself.
Well that’s pretty much it!
Share the love! Cheers! 😀