简体   繁体   English

Xamarin Forms Webview本地

[英]Xamarin Forms Webview local

I am trying to load a Web application (Readium) in a webview with Xamarin locally. 我正在尝试使用Xamarin在本地将Web应用程序(Readium)加载到Webview中。 As a target I have UWP, Android and iOS. 作为目标,我有UWP,Android和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. 我无法打开index.html页面,根据https://developer.xamarin.com/guides/xamarin-forms/user-interface/webview/ ,我已将Web嵌入到每个项目中,但是我得到了一个空白页。

have implemented the dependency service for each application such as (UWP) 已经为每个应用程序实现了依赖服务,例如(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 但是,使用NavigateToLocalStreamUri(uri,new StreamUriWinRTResolver())方法,创建一个新的UWP项目(不使用Xamarin),效果很好。

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? 在Xamarin表单中将以哪种方式进行相同操作? Thanks you. 谢谢。

Finally I achieve to load local content adding a custom render for each platform. 最后,我实现了加载本地内容并为每个平台添加自定义渲染的功能。

Example (UWP): 示例(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. 我按照Xamarin文档的示例使用针对3个平台的自定义渲染显示PDF。 I've tested it on Android and Windows: https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/ 我已经在Android和Windows上对其进行了测试: https//developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

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

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