使用WebView Xamarin表单加载本地HTML [英] Loading Local HTML with WebView Xamarin Forms

查看:309
本文介绍了使用WebView Xamarin表单加载本地HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xamarin表单在Web视图中加载本地HTML页面. 尽管我可以加载URL,但无法使用自己的HTML页面加载,但我在dev docs中使用的是基本示例.只需通过Android即可完成操作,因此无需担心IOS和Windows.

I am trying to load a local HTML page in a webview with Xamarin forms. I am using the basic example in the dev docs although I can get a URL to load I can't get my own HTML pages to load. This only needs to be done through Android so there is no worries about about IOS and Windows.

Xaml:

 <WebView
    x:Name="webviewjava"></WebView>

背后的代码:

 public partial class javscriptExample : ContentPage
{
    public interface IBaseUrl { string Get(); }
    public javscriptExample()
    {
        InitializeComponent();
        var source = new HtmlWebViewSource();

        source.BaseUrl = DependencyService.Get<IBaseUrl>().Get();

        webviewjava.Source = source;
    }
}

特定于平台的文件(LocalFile.cs): 只是要注意,这已被设置为Android资产.

The platform specific file (LocalFile.cs): Just to note this has been set as an Android asset.

 [assembly: Dependency(typeof(LocalFiles))]
namespace maptesting.Droid
{
    public class LocalFiles: IBaseUrl
    {
        public string Get()
        {
            return "file:///android_asset/";
        }

    }
}

在资产文件夹下有一个"TestWebPage.html",也已设置为Android资产.

and under the asset's folder there is a 'TestWebPage.html', also set as an Android asset.

尽管我不知道问题出在哪里,但我已通过调试将其放入,并且基本URL再次变空.为了清楚起见,我没有找到文件,屏幕只是空白. 此外,我不确定这是否有所作为. LocalFiles.cs文件中的"IBaseUrl"上没有突出显示语法.因此,我不确定它是否可以看到"它.

Although I dont know what the problem is I have put it through debug and the base url is coming back blank. Just to be clear im not getting a file not found, the screen is simply blank. Also, and Im not sure if this makes a difference. There is no syntax highlighting on 'IBaseUrl' in the LocalFiles.cs file. So I'm not sure if it can 'see' it.

有什么想法吗?

推荐答案

WebView.BaseUrl仅告诉WebView从哪里开始查找文件.它是网站"的根文件夹.默认情况下,浏览器将加载文件index.html,因此,如果将文件重命名为index.html,我相信它将自动加载.

WebView.BaseUrl only tells the WebView where to start looking for files. It's the root folder of the "web site". By default browsers will load the file index.html, so if you rename your file to index.html I believe it should load automatically.

我认为这也应该可行:

webviewjava.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
webviewjava.Source = "TestWebPage.html";

您在这里说的是使用此位置作为查找文件的默认位置"和查找此文件并将其用作HTML的来源".

Here you're saying "use this location as the default place to look for files" and "look up this file and use it as the source for the HTML".

这篇关于使用WebView Xamarin表单加载本地HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆