【问题标题】:Xamarin Forms - Displaying a local html file in webview on AndroidXamarin Forms - 在 Android 上的 webview 中显示本地 html 文件
【发布时间】:2018-01-11 10:41:39
【问题描述】:

听起来很简单,但由于某种原因,它不适合我。我有以下代码:

string rootPath = DependencyService.Get<IBaseUrl>().Get();
var source = new UrlWebViewSource
{
    Url = System.IO.Path.Combine(rootPath, "Index.html")
};

browser.Source = source;

IBaseUrl 在 android 上的实现代码是

public class BaseUrl : IBaseUrl
{
    public string Get()
    {
        return "file:///android_asset/Content/";

    }
}

还尝试从 url 中删除“内容”,但仍然无法正常工作。

【问题讨论】:

    标签: webview xamarin.forms


    【解决方案1】:

    您可以从 pcl 读取内容并创建 html 源代码:

    public static string GetFileContent(string fileName) {
                string content;
                var assembly = typeof(App).GetTypeInfo().Assembly;
                var stream = assembly.GetManifestResourceStream(fileName);
                if (stream == null) return null;
                using (var reader = new StreamReader(stream)) content = reader.ReadToEnd();
    
                return content;
            }
    
    void CreateSource(){
         var htmlSource = new HtmlWebViewSource();
         htmlSource.Html = GetFileContent("AppName.Resources.Content.html");
         browser.Source = htmlSource;
    }
    

    这适用于 iOS 和 Android。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多