xaml - 如何从跨平台 xamarin XAML 上的 Assets 目录加载图像?

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

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

  • 创建 Assets 目录
  • 添加图像集并将其命名为“imageName”(不带 .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.png"/>
    
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    我在 Assets 目录上添加了图像:

    Screenshot

    我正在 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 文件夹,仍然没有。

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

    Resources on folder

    还是没用。

    想法?

    编辑 2

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

    最佳答案

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

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

    Including the Asset Catalog folder in the project

    Creating a storyboard that comes with a code-behind

    关于xaml - 如何从跨平台 xamarin XAML 上的 Assets 目录加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47761269/

    相关文章:

    c# - 运行时的 Xamarin HttpClient 堆栈检测

    c# - 如何在 Xamarin.Forms XAML 中绘制路径

    c# - 如何在列表框中使用双向绑定(bind)?

    c# - 是否可以在 WPF 中实现工具栏并绘制控件?

    ios - 单色触摸 : Flurry Analytics

    c# - Messagebox.Show 和 DialogResult 在 MonoTouch 中等效

    xaml - Xamarin 表单 ListView 上下文操作不适用于详细信息

    carousel - Xamarin.Forms 轮播中的页面指示器,可能吗?

    c# - TemplateBinding 到自定义控件上的 DependencyProperty 不起作用

    c# - 如何在 .NET MAUI 中发送和显示获取请求?