如何从跨平台xamarin XAML的资产目录中加载图像? [英] How do I load an image from the Asset Catalog on cross-platform xamarin XAML?

查看:189
本文介绍了如何从跨平台xamarin XAML的资产目录中加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很容易做,而且从理论上讲,它看起来非常简单:

It seems like a really easy thing to do, and in theory it looks very straight forward:

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

或者至少这是我到处都读到的东西,但是我仍然无法使其正常工作.

or at least that's what i read pretty much everywhere, but I still can't get it to work.

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

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

I'm testing with Xamarin Live Player on an Iphone 6, and I can't see any image under the Label.

所以...我想念什么?

So... What am I missing?

编辑

适用于此人:是可以在Xamarin表单中使用图像集

但对我而言,我确实尝试删除.png扩展名并保留xaml:

But not for me, i did try removing the .png extension and leaving the xaml like this:

<?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文件夹,仍然没有.

Still doesn't work. Tried cleaning build folders and deleting bin/obj folders, still nothing.

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

Tried adding the resources to the Resources folder and deleting the Image Set from the Asset Catalog:

还是没用.

想法?

编辑2

我将这3张图片留在Resources文件夹中,并在XAML上添加了扩展名.png,然后它起作用了,仍然很高兴知道应该如何使用Asset Catalog完成它.

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.

推荐答案

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

We were testing on Xamarin Live Player on an IPhone, turns out Asset Catalogs are not yet supported. (https://twitter.com/praeclarum/status/941775333753217025?s=08)

感谢您的答复!

这篇关于如何从跨平台xamarin XAML的资产目录中加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆