如何从跨平台xamarin XAML上的资产目录中加载图像?

时间:2017-12-11 20:56:10

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

这似乎是一件非常容易的事,理论上看起来非常直接:

  1. 创建资产目录
  2. 添加图像集并将其命名为“imageName”(不包含.png)
  3. 添加图片
  4. 完成
  5. 或至少那是我几乎到处读的东西,但我仍然无法让它发挥作用。

    我的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>
    

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

    Screenshot

    我在Iphone 6上使用Xamarin Live Player进行测试,而且我看不到Label下的任何图像。

    所以...我错过了什么?

    修改

    适合这个人: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文件夹,但仍然没有。

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

    Resources on folder

    仍然无效。

    想法?

    编辑2

    我将这3张图片留在了资源文件夹上,并在XAML上添加了扩展名.png并且它有效,但仍然很高兴知道如何使用资产目录来完成它。

4 个答案:

答案 0 :(得分:1)

不要在XAML

中使用.png扩展名
<Image VerticalOptions="CenterAndExpand"
               HorizontalOptions="Center"
               Source="logo1"/>

答案 1 :(得分:1)

只是指出一些可能显而易见但却引起我多年问题的事情 - 您无法使用资产目录附带的LaunchImages集,因为它没有资格在图像下拉列表中列出。您必须创建一个新的图像集。

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

Including the Asset Catalog folder in the project

Creating a storyboard that comes with a code-behind

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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

感谢您的答复!

相关问题