【问题标题】:How do I load an image from the Asset Catalog on cross-platform xamarin XAML?如何从跨平台 xamarin XAML 上的资产目录加载图像?
【发布时间】:2018-05-25 11:14:40
【问题描述】:

这似乎是一件很容易做到的事情,理论上它看起来非常简单:

  1. 创建资产目录
  2. 添加一个图像集并将其命名为“imageName”(不带 .png)
  3. 添加图片
  4. 完成

或者至少这是我几乎到处都读到的内容,但我仍然无法让它工作。

我的 XAML 如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CrossBaiscs"
             x:Class="CrossBaiscs.MainPage">
    <ContentPage.Content>
        <StackLayout BackgroundColor="Orange">
            <Label Text="Welcome to Xamarin.Forms!"
                   TextColor="White"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />

            <Image VerticalOptions="CenterAndExpand"
                   HorizontalOptions="Center"
                   Source="logo1.png"/>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我在资产目录中添加了图片:

我正在 Iphone 6 上使用 Xamarin Live Player 进行测试,但在标签下看不到任何图像。

那么...我错过了什么?

编辑

它适用于这个人:Is it possible to use Image Set in a Xamarin Forms

但不适合我,我确实尝试删除 .png 扩展名并像这样保留 xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CrossBaiscs"
             x:Class="CrossBaiscs.MainPage">
    <ContentPage.Content>
        <StackLayout BackgroundColor="Orange">
            <Label Text="Welcome to Xamarin.Forms!"
                   TextColor="White"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />

            <Image VerticalOptions="CenterAndExpand"
                   HorizontalOptions="Center"
                   Source="logo1"/>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

还是不行。 尝试清理构建文件夹和删除 bin/obj 文件夹,仍然没有。

尝试将资源添加到资源文件夹并从资产目录中删除图像集:

还是不行。

想法?

编辑 2

我将这 3 张图片留在了 Resources 文件夹中,并在 XAML 上添加了扩展名 .png 并且它工作正常,仍然很高兴知道它应该如何使用资产目录来完成。

【问题讨论】:

标签: xaml xamarin.ios xamarin.forms cross-platform asset-catalog


【解决方案1】:

只是为了指出一些可能很明显但给我带来多年问题的问题 - 您不能使用资产目录附带的 LaunchImages 集,因为它不符合在图像下拉列表中列出的条件。您必须创建一个新的图像集。

在我这样做之后,以下答案突然起作用了(以前没有):

Including the Asset Catalog folder in the project

Creating a storyboard that comes with a code-behind

【讨论】:

    【解决方案2】:

    不要在 XAML 中使用 .png 扩展

    <Image VerticalOptions="CenterAndExpand"
                   HorizontalOptions="Center"
                   Source="logo1"/>
    

    【讨论】:

      【解决方案3】:

      在 Xamarin 文档中,他们已经明确提到 - 在 iOS 上,不能从资产目录图像集中的图像填充 Page.Icon 属性。而是从 iOS 项目的 Resources 文件夹中加载 Page.Icon 属性的图标图像。

      【讨论】:

        【解决方案4】:

        我们在 iPhone 上测试 Xamarin Live Player,结果发现资产目录尚不支持。 (https://twitter.com/praeclarum/status/941775333753217025?s=08)

        感谢您的回复!

        【讨论】:

          猜你喜欢
          • 2021-04-18
          • 2018-11-23
          • 2017-02-16
          • 2019-05-09
          • 1970-01-01
          • 2011-11-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多