0
votes

I am trying to load a Web application (Readium) in a webview with Xamarin locally. As a target I have UWP, Android and iOS. I can not get the index.html page open, I have embedded the web in each of the projects, according to https://developer.xamarin.com/guides/xamarin-forms/user-interface/webview/ but I get a blank page.

have implemented the dependency service for each application such as (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 works well, using the method NavigateToLocalStreamUri(uri, new StreamUriWinRTResolver()) with

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");
    }
}

In what way would the same be done in Xamarin Forms? Thanks you.

1

1 Answers

2
votes

Finally I achieve to load local content adding 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've followed the example of Xamarin docs to display PDF using custom render, targeting 3 platforms. I've tested it on Android and Windows: https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/