简体   繁体   English

Xamarin不允许加载本地资源文件

[英]Xamarin Not allowed to load local resource file

I have trouble with load page File.html. 我在加载页面File.html时遇到麻烦。 I want to load my map in html but emulator not shows. 我想以html格式加载地图,但未显示模拟器。 I got error : 我有错误:

" [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/HTMLPage1.html", source: data:text/html,chromewebdata (0) " “ [INFO:CONSOLE(0)]”不允许加载本地资源:file:///android_asset/HTMLPage1.html“,来源:data:text / html,chromewebdata(0)”

" I/chromium(11080): [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/webkit/android-weberror.png", source: data:text/html,chromewebdata (0) " “ I / chromium(11080):[INFO:CONSOLE(0)]”不允许加载本地资源:file:///android_asset/webkit/android-weberror.png“,来源:data:text / html,chromewebdata( 0)“

On emulator page shows "WebPage not available" 在模拟器页面上显示“ WebPage不可用”

Xaml file: XAML文件:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:Mapaht"
         x:Class="Mapaht.Mapahet">


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

Page file 页面文件

public Mapahet()
    {
        InitializeComponent();


        webviewjava.Source = "file:///android_asset/HTMLPage1.html";
    }

You are getting the WebView before setting the Content view so the wv is probably null. 在设置“内容”视图之前,您正在获取WebView,因此wv可能为null。

 protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
                SetContentView (Resource.Layout.webview);
                 WebView wv;  
                wv =  FindViewById<WebView>(Resource.Id.webviewjava);  
                wv.LoadUrl("file:///android_asset/HTMLPage1.html");   
            }  
        }

You need to have permissions in AndroidMainfest.xml file that has access to the internet: 您需要在有权访问互联网的AndroidMainfest.xml文件中具有权限:

 <uses-permission android:name="android.permission.INTERNET" />

I have trouble with load page File.html. 我在加载页面File.html时遇到麻烦。

Doing the following steps and it works fine on my side : 执行以下步骤,对我而言效果很好:

XAML : XAML:

<WebView
    x:Name="webviewjava" 
    HorizontalOptions="FillAndExpand" 
    VerticalOptions="FillAndExpand"
    />

Interface in your PCL : PCL中的接口

public interface IBaseUrl
{
    string Get();
}

Implement this interface in Android : 在Android中实现此接口

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

Load local resource file in Assets folder : 将本地资源文件加载到Assets文件夹中

public MainPage()
{
    InitializeComponent();

    var baseUrl = DependencyService.Get<IBaseUrl>().Get();
    string Url = $"{baseUrl}local.html";
    webviewjava.Source = Url;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM