Show local webpage in Xamarin.Forms WebView
Tags: html, convert, script
Published: 2021-12-01 09:48:19

###### A local webpage extracted from an online webpage that runs on HTML, CSS and JavaScript.


For my current [mobile app project](https://apps.maschinenring.de/meinacker/) I needed to embed the field map from an [website ](https://portal.maschinenring.de/) into my field management mobile app. Here you can see how it looks at the end of the process on both platforms:

![An image of the resulting map in my app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0q5g51h83i1ymqnm7fv.png)
###### Complex layouts and functions are often a problem in Xamarin.Forms … but they are great when they finally work 😇
 
There were multiple problems on the way:

* The webpage is running on HTML, CSS and JavaScript and executing custom code. We didn’t want to rewrite it in parallel in the web and the app, but to reuse the code.

So we shrinked down the web version to a local standalone page running in the webbrowser that can be displayed by simple opening the index.html file. For displaying it we would need to open the index file with the Xamarin.Forms WebView and make it load the other files.

* The webview is not easy to understand, because it’s cross-plattform but uses the native controls on each. It was hard to get it working on Android, but I didn’t make it on iOS.

It didn’t want to load the files from the assets, so I placed them in the shared project, set them as “Embedded Resource”, copied them at runtime to a sub folder in the cache and set this as the base path. Then I would load the index.html directly.

![Code snippet of how to load embedded resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ap59it4c1q2btz27san.png)
###### When the files are copied to [CacheDirectory]/Resources/AckerMap I set this directory as the base for the webview to search the other files referenced.

I got it working on android, but iOS was crashing and I still don’t know why.

So I was trying something else: If the webview doesn’t want to load additional files correctly, why not put everything into a single file? After a short search I found the Inliner tool. It takes your webpage and tries to combine all of the assets in a single file.

![Github repo of inliner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/061ir2rdwl9cp9jmk4qm.png)
###### From [Github](https://github.com/remy/inliner)

So I made sure my webpage runs well in my local browser and then used this tool to create the single file. It’s quite easy:

![Install command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pssarld82sc5wbtt7s8i.png)
###### Install the tool over NPM

![Inliner command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkt8mh1m9ecipsdz4gln.png)
 
###### Just specify the base html file of your webpage and the output file.

![The inliner cmd output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20ic7x3pak5irvmnserp.png)
 
###### And the output tells me everything is alright :-)

At last I just needed to put the file in my project, set it as “Embedded Resource” and load it directly:

![New embedded resource loading](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6sf9uzy9vevnaq24vnl.png)
 
And now 🥳 it works out of the box on Android & iOS 🎉💯

Have a nice day 😉