我有以下情况:
图书馆结构
Base.Library (.Net Standard 2.0 + Xamarin.Forms)
|- Views (XAML)
Base.Library.Android
|- Resources
|- drawables
|- icon.xml
Base.Library.iOS
|- Assets
|- icon (image asset with 3 resolutions)
Base.Library.Upw
|- Icons
|- icon.scale-100.png
|- ...
|- icon.scale-400.png
这些图标在iOS和Android上运行没有问题(在库项目的XAML页面和使用该库的最终应用程序项目中)。
<Image WidthRequest="24" HeightRequest="24" VerticalOptions="Center"
HorizontalOptions="End">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<On Platform="Android,iOS" Value="icon" />
<On Platform="UWP" Value="Icons/icon.png" />
</OnPlatform>
</Image.Source>
</Image>
只有UWP不起作用。是从库中加载图像的特殊语法吗?
我已经尝试过以下方法:
// Prefixed with assembly name of the library
Value="ms-appx:///Base.Library.Uwp/Icons/icon.png"
// Prefixed with namespace of the library
Value="ms-appx:///Base.Library.Uwp.Namespace/Icons/icon.png"
我找到了解决方案。
如果我在值中有ms-appx:///
,则将其视为UriImageSource
而不是FileImageSource
。如果我将其剪掉并使用图标的完整路径(包括程序集名称),则还会显示UWP的图标。
<Image WidthRequest="24" HeightRequest="24" VerticalOptions="Center"
HorizontalOptions="End">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<On Platform="Android,iOS" Value="icon" />
<!-- Icon name prefixed with assembly name -->
<On Platform="UWP" Value="Base.Library.Uwp/Icons/icon.png" />
</OnPlatform>
</Image.Source>
</Image>