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

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

问题描述

我正在尝试使用 Xamarin 表单在 web 视图中加载本地 HTML 页面.我正在使用开发文档中的基本示例,尽管我可以获得要加载的 URL,但我无法加载自己的 HTML 页面.只需通过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.

我认为这也应该是可能的:

I think this should be possible too:

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 Forms 加载本地 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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