Local html file as webview.source

Hello still learning here, I have a local html file it's already placed in the assets folder inside another foler called webview, I'm following the guide provided at https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview, basically I'm using it to show a background video since I couldn't find a better way to do so in android.

The example as I understand, provides a way to fill the WebView with an HtmlWebViewSource

        var browser = new WebView();
        var htmlSource = new HtmlWebViewSource();

        htmlSource.Html = @"<html>
                            <head>
                            <link rel=""stylesheet"" href=""default.css"">
                            </head>
                            <body>
                            <h1>Xamarin.Forms</h1>
                            <p>The CSS and image are loaded from local files!</p>
                            <img src='XamarinLogo.png'/>
                            <p><a href=""local.html"">next page</a></p>
                            </body>
                            </html>";

        htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
        browser.Source = htmlSource;
        Content = browser;

But I just need to do something like WebVideo.Source = Url + "rain" + ".html";
The url is just string Url = "webview/";

The BaseUrl_Android class is already at the android folder and if I use the url in such way it appears like a blank page, all assets are set as androidasset in it's properties
it seems I don't know how to use these lines:

    htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
        browser.Source = htmlSource;

Any pointer here will be reatly appreciatted, thanks.

Best Answer

  • Accepted Answer

    Sorry guys now I get it a little bit more, I wish it was easier to find some detailed information about the definitions like specifically what do they do or I don't, sometimes I feel like I'm copy/pasting with no more explanation :(
    here's what I used:

            string url = DependencyService.Get<IBaseUrl>().Get();
            string TempUrl = Path.Combine(url, "webview/night.html");
            urlSource.Url = TempUrl;
            WebVideo.Source = urlSource; 
    

    Didn't know precisely what I was looking for but I got the details from here https://forums.xamarin.com/discussion/149316/how-to-add-path-to-webview

Answers

  • jointothedarksidejointothedarkside Member ✭✭
    Accepted Answer

    Sorry guys now I get it a little bit more, I wish it was easier to find some detailed information about the definitions like specifically what do they do or I don't, sometimes I feel like I'm copy/pasting with no more explanation :(
    here's what I used:

            string url = DependencyService.Get<IBaseUrl>().Get();
            string TempUrl = Path.Combine(url, "webview/night.html");
            urlSource.Url = TempUrl;
            WebVideo.Source = urlSource; 
    

    Didn't know precisely what I was looking for but I got the details from here https://forums.xamarin.com/discussion/149316/how-to-add-path-to-webview

Sign In or Register to comment.