【问题标题】:Load icons from UWP-library as image in Xamarin.Forms将 UWP 库中的图标加载为 Xamarin.Forms 中的图像
【发布时间】:2018-08-08 02:12:21
【问题描述】:

我有以下情况:

  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 上正常工作(在 ibrary 项目的 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】:

    我找到了解决办法。

    如果值中有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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-04
      • 2016-06-25
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多