代码之家  ›  专栏  ›  技术社区  ›  pavel

用于读取PDF的自定义xamarin.forms.webview。带定位点滚动

  •  2
  • pavel  · 技术社区  · 8 年前

    我试图找到在xamarin.forms上显示本地PDF文件的方法。 我发现了一个实现WebView及其呈现的自定义实现的解决方案: pdf reader

    主要代码是:

    public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty = BindableProperty.Create<CustomWebView, string>(p => p.Uri, default(string));
    
        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }
    }
    

    提供:

    [assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
    namespace DisplayPDF.iOS
    {
        public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
        {
            protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
            {
                base.OnElementChanged (e);
    
                if (Control == null) {
                    SetNativeControl (new UIWebView ());
                }
                if (e.OldElement != null) {
                    // Cleanup
                }
                if (e.NewElement != null) {
                    var customWebView = Element as CustomWebView;
                    string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
                    Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
                    Control.ScalesPageToFit = true;
                }
            }
        }
    }
    

    我的网页:

    public class WebViewPageCS : ContentPage
        {
            public WebViewPageCS ()
            {
                webView = new CustomWebView
                {
                    Uri = "ScalaReference.pdf",
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions = LayoutOptions.FillAndExpand
                };
        }
    

    但是现在我找不到向这个PDF文件添加锚定的方法,如本文所述: anchor to pdf

    我还尝试使用此代码来评估脚本:

    private async void Scroll_Clicked(object sender, EventArgs e)
    {
          webView.Eval($"window.scrollTo(0, {x});");
    }
    

    它可以在默认的WebView中正常工作,但不能在自定义的WebView中正常工作。

    也许有人知道其他方法可以滚动/设置锚定为PDF并通过xamarin.forms链接到此锚定? 谢谢您。

    1 回复  |  直到 8 年前
        1
  •  1
  •   ColeX    8 年前

    它可以在默认的WebView中正常工作,但不能在自定义的WebView中正常工作。

    它不是由自定义WebView引起的,因为渲染器为 Control 在里面 CustomWebViewRenderer ,查看此代码部分:

    if (Control == null) {
       SetNativeControl (new UIWebView ());
    }
    

    所以,它在执行时不工作 webView.Eval($"window.scrollTo(0, {x});"); ,因为此WebView实际上不是uiWebView。

    解决方案

    在CustomWebView中创建BindableProperty

    public static readonly BindableProperty AnchorProperty = BindableProperty.Create<CustomWebView, float>(p => p.Anchor, default(float));
    
    public float Anchor
    {
        get { return (float)GetValue(AnchorProperty); }
        set { SetValue(AnchorProperty, value); }
    }
    

    在页面中触发它

    private void Scroll_Clicked(object sender, System.EventArgs e)
    {
        webview.Anchor = 200;
    }
    

    获取渲染器中的值并使WebView滚动。

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    
        if(e.PropertyName is "Anchor")
        {
            var customWebView = Element as CustomWebView;
            float anchor = customWebView.Anchor;
            Control.ScrollView.ContentOffset = new CoreGraphics.CGPoint(0, anchor);
        }
    }