从UWP库中加载图标作为Xamarin.Forms中的图像

问题描述 投票:0回答:1

我有以下情况:

  1. 我们有/构建一个Xamarin.Forms库,它也应该包含我们常用的图标
  2. 此图标应在此库中的XAML年龄段中使用,但也应在最终应用程序中使用

图书馆结构

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"
image xamarin.forms uwp resources xamarin.uwp
1个回答
0
投票

我找到了解决方案。

如果我在值中有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>
© www.soinside.com 2019 - 2024. All rights reserved.