简体   繁体   English

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

[英]How do I load an image from the Asset Catalog on cross-platform xamarin XAML?

It seems like a really easy thing to do, and in theory it looks very straight forward: 这似乎是一件非常容易的事情,理论上它看起来很直接:

  1. create the Asset Catalog 创建资产目录
  2. add an Image Set and name it "imageName" (without .png) 添加图像集并将其命名为“imageName”(不带.png)
  3. add the images 添加图像
  4. done DONE

or at least that's what i read pretty much everywhere, but I still can't get it to work. 或者至少那是我几乎到处读的东西,但我仍然无法让它发挥作用。

My XAML looks like this: 我的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>

And I added the images on the Asset Catalog: 我在资产目录中添加了图像:

截图

I'm testing with Xamarin Live Player on an Iphone 6, and I can't see any image under the Label. 我在Iphone 6上使用Xamarin Live Player进行测试,我看不到Label下的任何图像。

So... What am I missing? 那么......我错过了什么?

EDIT 编辑

It works for this guy: Is it possible to use Image Set in a Xamarin Forms 它适用于这个人: 是否可以在Xamarin表格中使用图像集

But not for me, i did try removing the .png extension and leaving the xaml like this: 但不适合我,我确实尝试删除.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>

Still doesn't work. 仍然无法正常工作。 Tried cleaning build folders and deleting bin/obj folders, still nothing. 尝试清理构建文件夹和删除bin / obj文件夹,仍然没有。

Tried adding the resources to the Resources folder and deleting the Image Set from the Asset Catalog: 尝试将资源添加到Resources文件夹并从资产目录中删除图像集:

文件夹上的资源

Still didn't work. 仍然没有奏效。

Ideas? 想法?

EDIT 2 编辑2

I left those 3 images on Resources folder and added the extension .png on XAML and it worked, still would be nice to know how its supposed to be done using the Asset Catalog. 我把这3张图片留在了Resources文件夹上,并在XAML上添加了扩展名.png,但它确实很有用,知道如何使用资产目录完成它。

Just to point out something that's probably obvious but caused me issues for ages - you can't use the LaunchImages set that comes with the asset catalog as it is not eligible to list in the Image dropdown. 只是指出一些可能显而易见但却引起我多年问题的事情 - 您无法使用资产目录附带的LaunchImages集,因为它没有资格在图像下拉列表中列出。 You have to create a new Image Set. 您必须创建一个新的图像集。

After I did this, the following answers suddenly worked (where they hadn't before): 在我这样做之后,以下答案突然起作用(之前没有):

Including the Asset Catalog folder in the project 在项目中包含Asset Catalog文件夹

Creating a storyboard that comes with a code-behind 创建一个带有代码隐藏的故事板

don't use the .png extension in the XAML 不要在XAML中使用.png扩展名

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

In Xamarin Documentation they have clearly mentioned that- On iOS, the Page.Icon property can't be populated from an image in an asset catalog image set. 在Xamarin文档中,他们清楚地提到过 - 在iOS上,无法从资产目录图像集中的图像填充Page.Icon属性。 Instead, load icon images for the Page.Icon property from the Resources folder in the iOS project. 而是从iOS项目的Resources文件夹中加载Page.Icon属性的图标图像。

We were testing on Xamarin Live Player on an IPhone, turns out Asset Catalogs are not yet supported. 我们在iPhone上测试Xamarin Live Player,结果证明资产目录尚不支持。 ( https://twitter.com/praeclarum/status/941775333753217025?s=08 ) https://twitter.com/praeclarum/status/941775333753217025?s=08

Thanks for the replies! 谢谢你的回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Xamarin Forms 在 a.xaml 文件中获取跨平台应用程序中的可用屏幕尺寸值? - How to Get The Avaible Screen Size Value In Cross-Platform App From Xamarin Forms in a .xaml File? XAML图像源无法在跨平台上使用(Android模拟器) - XAML Image source not work in cross-platform (Android emulator) 了解跨平台应用程序如何启动和使用xaml页面 - Understanding how Cross-Platform App starts and uses xaml pages 如何在Xamarin Forms跨平台中绑定图像集合 - How to bind collection of images in Xamarin Forms Cross-Platform Intellisense在Xamarin.Forms XAML跨平台移动应用程序的Visual Studio 2013中不起作用 - Intellisense not working in Visual Studio 2013 for Xamarin.Forms XAML Cross-Platform Mobile App Xamarin.Forms:使用资产目录中的图像 - Xamarin.Forms: Use image from asset catalog 无法在我的Xamarin跨平台应用程序中正确加载Google Maps - Unable to load Google Maps Properly in my Xamarin Cross-Platform App 如何在Xamarin Cross Platform xaml中添加评级栏 - How to Add Rating Bar in Xamarin Cross Platform xaml Xamarin标题栏文本居中(跨平台) - Xamarin Title bar text centered (Cross-platform) Xamarin形式(跨平台):ListView中的多种单元格类型 - Xamarin forms (Cross-Platform) : Multiple type of cells in ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM