I am working on an Android application using Xamarin but hopefully the question below is more general and can be answered also by people not familiar with Xamarin. The app is very simple and has one main page with a list where the use can click on any of the items. These items represent products and all the related information are stored into a dictionary (key is the product name, values are some information about the product). Amongst the available fields, I also added a URL of the website where the user can access more information about the product. The app has a WebView to display the page inside the app.
I can display the webpage by using:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
web.LoadUrl(item.Link);
where the item/product the user has selected is the item object having the URL saved in item.Link. The problem is that the user need a web connection in order to load the page. So I thought about saving the whole HTML code into a string so that the user can visualize the content even if no connection is available. I am doing this by:
WebClient wc = new WebClient();
using (Stream st = wc.OpenRead(web.Url))
{
using (StreamReader sr = new StreamReader(st, Encoding.UTF8))
{
html = sr.ReadToEnd();
}
}
Then each item in the dictionary will have a field item.Html with the source code of the page. I can visualize the page with the following code:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
//web.LoadUrl(item.Link);
web.LoadData(item.Html, "text/html", "UTF-8");
The problem is that the viewer does not display the page in the same way as web.LoadUrl(item.Link) would do (i.e. the page as you would see in any web browser) but it is much messier and difficult to navigate. What am I doing wrong? Any better way to achieve my goal?