如何在Xamarin Forms中的WebView中加载本地HTML文件

5

我打算将本地HTML文件加载到Xamarin Forms WebView中。

我已经将HTML文件添加到“assets/HTML”文件夹中,其构建操作设置为AndroidAsset。

我已经实现了基本的URL查找器接口:

public class BaseUrl_Android:IBaseUrl
    {
        public string Get()
        {
            return "file:///android_asset/";
        }
    }

我曾尝试通过在共享便携代码中使用以下内容来加载文件:
   string baseUrl = DependencyService.Get<IBaseUrl>().Get();
   string url = baseUrl + "HTML/local.html";
   myWebView.Source = url;

我也尝试了自定义的WebViewRenderer,并使用了以下代码:

protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            // lets get a reference to the native control
            var webView = (global::Android.Webkit.WebView)Control;
            // do whatever you want to the WebView here!                                
            webView.LoadUrl(DependencyService.Get<IBaseUrl>().Get()+"HTML/local.html");
            webView.SetWebViewClient(new MyWebViewClient());
        }

使用这两种解决方案都会出现错误:

enter image description here

还有什么其他尝试的方法吗?

4个回答

0
你的源码应该是HtmlWebViewSource类型而不是字符串。
var html = new HtmlWebViewSource ();
html.BaseUrl = DependencyService.Get<IBaseUrl> ().Get ();

0

这是我的MainActivity.cs文件;

WebView web_view;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    web_view = FindViewById<WebView>(Resource.Id.webview);
    web_view.Settings.JavaScriptEnabled = true;

    web_view.LoadUrl("file:///android_asset/Home.html");

}

你必须将你的HTML文件放入Assets文件夹中,并将HTML构建操作设置为AndroidAsset。通过这种方式,我可以将HTML文件加载到WebView中。


您的解决方案基于工作模板,所提出的问题属于“空白应用程序(Xamarin.Forms Portable)”。 - Ashish-BeJovial

0

我不确定为什么你遇到了麻烦。 我按照你的步骤操作,一切都正常:

代码片段如下:

public ViewModel()
{
    var rootUrl = DependencyService.Get<INativeURL>().Acquire();
    Url = $"{rootUrl}content.html";
}

注意:

“Content.html”是我添加到本地Droid项目的“Assets”文件夹中的本地网页的名称。

XAML

<ContentPage.BindingContext>
    <local:ViewModel/>
</ContentPage.BindingContext>

<WebView Source="{Binding Url}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />

视图模型:

    public ViewModel()
    {
        var rootUrl = DependencyService.Get<INativeURL>().Acquire();
        Url = $"{rootUrl}content.html";
    }

    string _url = null;
    public string Url
    {
        get
        {
            return _url;
        }
        set
        {
            if (_url != value)
            {
                _url = value;
                OnNotifyPropertyChanged();
            }
        }
    }

接口:

public interface INativeURL
{
    string Acquire();
}

Android 合约:

[assembly: Dependency(typeof(NativeURL_Android))]
namespace my_namespace.Droid
{
    public class NativeURL_Android : INativeURL
    {
        public string Acquire() => "file:///android_asset/";
    }
}

什么是DependencyService?它是一个类吗?如果是,那么我应该在哪里创建它,并且需要写入什么内容? - Ashish-BeJovial

0

这里有相关的帖子和官方示例

看看这是否有所帮助。

Xamarin 加载本地 HTML

以及

官方示例在这里

public LocalHtmlBaseUrl ()
    {
        var browser = new BaseUrlWebView (); // temporarily use this so we can custom-render in iOS

        var htmlSource = new HtmlWebViewSource ();

        htmlSource.Html = @"<html>...</html>";

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


        browser.Source = htmlSource;

        Content = browser;
    }

请添加示例链接! - Max CHien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接