简体   繁体   中英

Xamarin Not allowed to load local resource file

I have trouble with load page File.html. I want to load my map in html but emulator not shows. I got error :

" [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/HTMLPage1.html", source: 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) "

On emulator page shows "WebPage not available"

Xaml file:

<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.

 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:

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

I have trouble with load page File.html.

Doing the following steps and it works fine on my side :

XAML :

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

Interface in your PCL :

public interface IBaseUrl
{
    string Get();
}

Implement this interface in 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 :

public MainPage()
{
    InitializeComponent();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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