Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Local HTML file does not show properly in WebView with Xamarin.Forms

I have a simple HTML file:

<html>
  <head>
    <script async src="https://URL"></script>
  </head>
  <body>
    <div class="class-name"></div>
  </body>
</html>

When I access it with a browser using Windows: C:\foo.html?q=query the file loads correctly.

When I access the same file, that I have added it to my Android project in the Assets directory, with a Custom WebView and a Custom Renderer in my Xamarin.Forms project like this: myCustomWebview.Uri = "foo.html" + "?q=" + queryString; the Javascript fails to populate the div in the body correctly. The same problem exists if I load the file without a query string: myCustomWebview.Uri = "foo.html";

What's the deal? Do I need to set any special Permissions in the AndroidManifest.xml for the Javascript to work properly? Do I need to set a specific BuildAction in the file properties of the HTML file in my Android project? Do I need to do something else?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Place html file into Assets directory with build action AndroidAsset .

  2. Use DependencyService to set the exact path .

    [assembly: Dependency (typeof(BaseUrl_Android))]
    namespace WorkingWithWebview.Android
    {
        public class BaseUrl_Android : IBaseUrl
        {
            public string Get()
            {
                return "file:///android_asset/";
            }
        }
    }
    
  3. Set BaseUrl to HtmlWebViewSource and pass it to WebView.Source .

    var source = new HtmlWebViewSource();
    source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
    webView.Source  = source .
    

Refer to

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#local-html-content.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!