这似乎是一件非常容易的事情,理论上它看起来非常简单:
或者至少这是我在任何地方读到的,但我仍然无法让它工作。
我的 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 目录上添加了图像:
我正在 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 目录中删除图像集:
还是没用。
想法?
编辑 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/