Xamarin Forms Local Webview

0

I am trying to load a web application (Readium) in a Webview with Xamarin.Forms locally. As a target I have UWP, Android and iOS.

I can not get the page index.html open, I have embedded the Web in each of the projects, according to link but I get a blank page.

I have implemented the dependency service for each application of the form (UWP)

assembly: Dependency(typeof(BaseUrl))]
namespace WorkingWithWebview.UWP
{
    public class BaseUrl : IBaseUrl
    {
        public string Get()
        {
            return "ms-appx-web:///";
        }
    }
}

However, creating a new UWP project (without Xamarin), it does work well for me to load the application, using the NavigateToLocalStreamUri (uri, new StreamUriWinRTResolver ()) method with

public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
    {
        if (uri == null)
        {
            throw new Exception();
        }
        string path = uri.AbsolutePath; 
        return GetContent(path).AsAsyncOperation();
    }

    private async Task<IInputStream> GetContent(string path)
    {  
        try
        {
            Uri localUri = new Uri("ms-appx:///cloud-reader" + path);
            StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
            IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
            return stream;
        }
        catch (Exception)
        {
            throw new Exception("Invalid path");
        }
    }
}

How would the same thing be done in Xamarin.Forms? Thanks in advance.

    
asked by WP8_CT 16.05.2017 в 20:00
source

1 answer

0

In the end I was able to load the web locally using a custom render for each platform.

Example (UWP):

[assembly: ExportRenderer(typeof(WebView), typeof(WebViewRenderer))]
namespace DisplayEpub.UWP{
public class CustomWebViewRenderer : WebViewRenderer
{
   protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
   {
       base.OnElementChanged(e);

       if (e.NewElement != null)
       {
           var customWebView = Element as WebView;
           Control.Source = new Uri(string.Format("ms-appx-web:///Assets/pdfjs/web/viewer.html"));
       }
   }
}
}

I followed the example of the Xamarin documentation to show PDF in the webview, in the 3 platforms using custom render. I've tried it on Android and Windows: link

    
answered by 25.05.2017 / 11:57
source