【问题标题】:(Xamarin.Forms IOS) PDF image not displaying(Xamarin.Forms IOS) PDF 图像不显示
【发布时间】:2021-10-11 07:35:27
【问题描述】:

我在 Xamrin Forms 中创建了一个跨平台应用程序。该应用程序在 Android 和 UWP 中完美运行。但在 IOS 中加载时,图标不可见。
例如

<Image >
      <Image.Source>
          <OnPlatform x:TypeArguments="FileImageSource">
              <On Platform="Android" Value="login_icon" />
              <On Platform="UWP" Value="Images/login_icon.png" />
              <On Platform="iOS" Value="login_icon" />
          </OnPlatform>
       </Image.Source>
</Image>   

我已将 PDF 格式 的所有图像添加到 Assest Catalog 中。图像的大小也正确。但由于某种原因,图标没有显示。
但是,如果我将这些图像添加到 Resources as .p​​ng 中,它们就会出现。官方文件说这种方法已被弃用。

编辑: 在模拟器中一切看起来都不错,但是当我使用真实设备进行测试时,这些图标不可见。

【问题讨论】:

  • 在文档Displaying an Image in Xamarin IOS 中,注意Using vector images in asset catalogs 部分:“PDF 格式的矢量 图像”。 PDF 是否包含矢量(不是由像素组成的位图)?如果您有 (jpg/png) 位图,推荐的方法是创建一个图像集,您可以在其中添加不同分辨率的图像。
  • @ToolmakerSteve 我有一个 svg 图像,我可能会尝试将其转换为 PDF 并重试。
  • 这样行吗?
  • @AdrainZhu-MSFT 我必须先创建一个合适大小的矢量图像。我会在测试后立即更新
  • @ToolmakerSteve 仍然没有运气,但是所有图标都在模拟器中可见。当我使用设备进行测试时,这些图标不可见

标签: xaml xamarin.forms xamarin.ios


【解决方案1】:

您可以使用 CutomRenderer 创建自定义 webview 来显示 pdf 文件。

代码如下:

    public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty = BindableProperty.Create (propertyName:"Uri",
                returnType:typeof(string),
                declaringType:typeof(CustomWebView),
                defaultValue:default(string));
    
        public string Uri {
            get { return (string)GetValue (UriProperty); }
            set { SetValue (UriProperty, value); }
        }
    }

在IOS中:

   [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;
                }
            }
        }
    }

这里是关于customrendererhttps://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/introduction的文档

【讨论】:

  • 我不想显示 PDF。我的图标是 PDF 格式,这些图标是不可见的。
猜你喜欢
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 2019-05-25
  • 2019-01-08
相关资源
最近更新 更多