【问题标题】:Xamarin.Forms Ios imageXamarin.Forms Ios 映像
【发布时间】:2019-09-22 16:01:34
【问题描述】:

我在 xamarinforms 中使用图像并绑定来自远程 uri 的源,它在 android 中工作,但在 ios 中,图像根本不显示。请有人帮我解决这个问题。

【问题讨论】:

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

问题在于 android 和 iOS 的命名约定,因此请确保命名。 尝试使用 svg 以方便使用。

https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_images/sizes-and-filenames/

【讨论】:

  • Take the Tour,并务必阅读How do I ask a good question?。尝试提供有关您的问题的详细信息。添加您的代码,您面临什么问题? ,你的阻拦在哪里? ,到目前为止你尝试了什么?就像你提出问题的方式一样。
【解决方案2】:

在 ios 中,如果您从 http 加载图像,则必须在 info.plist 文件中添加 NSAppTransportSecurity 才能正常工作。

默认情况下ios从安全url加载图片(以https开头)

你可以参考这个 How do I load an HTTP URL with App Transport Security enabled in iOS 9?

【讨论】:

    【解决方案3】:

    如果您在 Code behind 中执行此操作:

    ImageSource.FromFile("ImageName.jpg") // from local source,
    ImageSource.FromUri(new Uri(url)) // from remote source.
    

    如果您尝试从 XAML 进行绑定,您可以使用转换器并返回图像;

    <ContentView.Resources>
            <ResourceDictionary>
                <converters:ImageConverter x:Key="ImageConverter" />
            </ResourceDictionary>
    </ContentView.Resources>
    
    <Image Grid.Row="1" Grid.Column="0" Source="{Binding YourObjectHere, Converter={StaticResource ImageConverter}}" />
    

    在转换器类中:

        public class ImageConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value != null)
                {
                    if (value is YourObject xxx)
                    {
                        var url = xxx.Images?["small"]; // This is custom code here, you build your url
    
                        return string.IsNullOrEmpty(url) ? ImageSource.FromFile("NoImage.jpg") : ImageSource.FromUri(new Uri(url));
                    }
                }
    
                return ImageSource.FromFile("NoImage.jpg");
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2021-10-11
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2016-01-11
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多