如何引用 XAML 中具有不同文件扩展名的图像?
How to reference images with different file extensions from XAML?
我们开发了一个 Xamarin.Forms UI 库,其中图像存储为 Android 的 PNG 和 iOS 的 PDF。对于 Android,图像位于 Resources 文件夹中,对于 iOS,图像位于资产目录中。 iOS 和 Android 之间每个图像的文件名相同,但文件扩展名不同。
使用 xcrun --sdk iphoneos assetutil --info Assets.car
检查 Assets.car 显示图像存储为 PDF,在构建时未转换为 PNG。
...
{
"AssetType" : "Image",
"BitsPerComponent" : 8,
"ColorModel" : "RGB",
"Colorspace" : "srgb",
"Compression" : "deepmap2",
"Encoding" : "ARGB",
"Image Type" : "kCoreThemeOnePartScale",
"Name" : "ic_cup_coffee",
"NameIdentifier" : 54350,
"Opaque" : false,
"PixelHeight" : 24,
"PixelWidth" : 24,
"RenditionName" : "ic_cup_coffee.pdf",
"Scale" : 1,
"SHA1Digest" : "0999A4B02703DF4A5FC84AF67FEF220E560F79DD",
"SizeOnDisk" : 334,
"Template Mode" : "automatic"
}
...
为了引用来自 XAML 的图像,我们有一个助手 class 将 class 字段映射到不包括文件扩展名的文件名:
public static class Images
{
public static readonly string Ic_cup_coffee = "ic_cup_coffee"; // value equals file name
...
}
在 XAML 中,我们使用 x:Static 标记扩展:
<Image Source="{x:Static images:Images.Ic_cup_coffee}" />
我们在使用这种方法时遇到的问题是 有时 图像不会显示在集成了 Xamarin.Forms [=46] 的 iOS 应用程序中=] 库作为 NuGet 包。 Android 我们没有遇到任何问题。
documentation 说明了如何在不同平台上使用单个图像文件,但没有说明如何处理用于同一图像源的具有不同文件扩展名的文件。
如何正确引用 XAML 中不同文件扩展名的图片?我们参考图像的方法有效吗?
可以像这样从 XAML 中引用具有不同文件扩展名的图像:
<Image>
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS><FileImageSource File="ic_cup_coffee.pdf"/></OnPlatform.iOS>
<OnPlatform.Android><FileImageSource File="ic_cup_coffee.png"/></OnPlatform.Android>
</OnPlatform>
</Image.Source>
</Image>
无论如何,这并不能解决我们有时看不到图片的问题。我们坚持使用<Image Source="ic_cup_coffee"/>
,我们将分析这个bug是否是问题的根源。
我们开发了一个 Xamarin.Forms UI 库,其中图像存储为 Android 的 PNG 和 iOS 的 PDF。对于 Android,图像位于 Resources 文件夹中,对于 iOS,图像位于资产目录中。 iOS 和 Android 之间每个图像的文件名相同,但文件扩展名不同。
使用 xcrun --sdk iphoneos assetutil --info Assets.car
检查 Assets.car 显示图像存储为 PDF,在构建时未转换为 PNG。
...
{
"AssetType" : "Image",
"BitsPerComponent" : 8,
"ColorModel" : "RGB",
"Colorspace" : "srgb",
"Compression" : "deepmap2",
"Encoding" : "ARGB",
"Image Type" : "kCoreThemeOnePartScale",
"Name" : "ic_cup_coffee",
"NameIdentifier" : 54350,
"Opaque" : false,
"PixelHeight" : 24,
"PixelWidth" : 24,
"RenditionName" : "ic_cup_coffee.pdf",
"Scale" : 1,
"SHA1Digest" : "0999A4B02703DF4A5FC84AF67FEF220E560F79DD",
"SizeOnDisk" : 334,
"Template Mode" : "automatic"
}
...
为了引用来自 XAML 的图像,我们有一个助手 class 将 class 字段映射到不包括文件扩展名的文件名:
public static class Images
{
public static readonly string Ic_cup_coffee = "ic_cup_coffee"; // value equals file name
...
}
在 XAML 中,我们使用 x:Static 标记扩展:
<Image Source="{x:Static images:Images.Ic_cup_coffee}" />
我们在使用这种方法时遇到的问题是 有时 图像不会显示在集成了 Xamarin.Forms [=46] 的 iOS 应用程序中=] 库作为 NuGet 包。 Android 我们没有遇到任何问题。
documentation 说明了如何在不同平台上使用单个图像文件,但没有说明如何处理用于同一图像源的具有不同文件扩展名的文件。
如何正确引用 XAML 中不同文件扩展名的图片?我们参考图像的方法有效吗?
可以像这样从 XAML 中引用具有不同文件扩展名的图像:
<Image>
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS><FileImageSource File="ic_cup_coffee.pdf"/></OnPlatform.iOS>
<OnPlatform.Android><FileImageSource File="ic_cup_coffee.png"/></OnPlatform.Android>
</OnPlatform>
</Image.Source>
</Image>
无论如何,这并不能解决我们有时看不到图片的问题。我们坚持使用<Image Source="ic_cup_coffee"/>
,我们将分析这个bug是否是问题的根源。