visual-studio - 嵌入图像未在 Xamarin.Forms 中显示

标签 visual-studio xamarin.forms xamarin.ios xamarin.android

我的“图像”文件夹中有两张图像。尝试将这些图像加载到页面中,但图像未显示在页面上。它在调试时不会产生任何错误。我正在检查 Xamarin Live Player Android 设备。

ImageResourceExtension.cs

    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    using Xamarin.Forms.Internals;

    namespace Buffting
    {
        // You exclude the 'Extension' suffix when using in Xaml markup
        [Preserve(AllMembers = true)]
        [ContentProperty(nameof(Source))]
        public class ImageResourceExtension : IMarkupExtension
        {
            public string Source { get; set; }
            public object ProvideValue(IServiceProvider serviceProvider)
            {
                if (Source == null)
                    return null;
                // Do your translation lookup here, using whatever method you require
                var imageSource = ImageSource.FromResource(Source);
                return imageSource;
            }
        }
    }

BeautySalon.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:Buffting;assembly=Buffting"
             x:Class="Buffting.Home.BeautySalon"
             Title="Beauty Salon">
    <ContentPage.Content>
        <ListView  x:Name="SalonList" RowHeight="370">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Frame Margin="5,5,5,-5" OutlineColor="Blue" HasShadow="true">
                            <Grid>
                                <Grid Grid.Row="0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="200"   />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Image Grid.Row="0" Grid.ColumnSpan="2"  HeightRequest="200"  Source="{ Binding SalonImage}"  VerticalOptions="Fill"  Aspect="AspectFill"  />
                                </Grid>
                                <Grid Grid.Row="1" Padding="10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Row="1" Grid.Column="0" Margin="0, 40, 0, 0" Text="{ Binding SalonName }" FontSize="Large" TextColor="White" />
                                    <Image Grid.Row="1" Grid.Column="1" x:Name="Star" Source="{local:ImageResource Buffting.Images.star_outline.png}" />
                                </Grid>

                                <Grid Grid.Row="2" Padding="0, 0, 10, 0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Label Grid.Row="0" Grid.Column="0" Text="Location:" FontAttributes="Bold" FontSize="Small" TextColor="Black" />
                                    <Label Grid.Row="0" Grid.Column="1" Text="{ Binding Location}"  />
                                    <Label Grid.Row="1" Grid.Column="0" Text="Full Address:" FontAttributes="Bold" FontSize="Small" TextColor="Black"  />
                                    <Label Grid.Row="1" Grid.Column="1" Text="{ Binding Address}"  />
                                    <Label Grid.Row="2" Grid.Column="0" Text="Phone:" FontAttributes="Bold" FontSize="Small" TextColor="Black"  />
                                    <Label Grid.Row="2" Grid.Column="1" Text="{ Binding Phone}"  />
                                    <Label Grid.Row="3" Grid.Column="0" Text="Opening Time:" FontAttributes="Bold" FontSize="Small" TextColor="Black"  />
                                    <Label Grid.Row="3" Grid.Column="1" Text="{ Binding OpeningTime}"  />
                                </Grid>
                            </Grid>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

我正在使用此代码,但图像未加载。我是 Xamarin 新手,请指导我。

最佳答案

这是错误的,图像被放置在每个 native 项目中。对于 Xamarin.Forms,您应该将图像放置在 Drawables 文件夹中。

如果您的项目是 android 和 ios,您最终将拥有两张图像:一张位于 iosproject/Resources 中,一张位于 AndroidProject/Resources/Drawab 中。

参见Images Doc

关于visual-studio - 嵌入图像未在 Xamarin.Forms 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465009/

相关文章:

visual-studio-2010 - VS2010 F# Smart Tag 添加开放导入声明

visual-studio - 如何在不加载其所有项目的情况下打开 Visual Studio 解决方案?

xaml - 如何将命令绑定(bind)到 Xamarin 表单中的条目 "Unfocused"事件

ios - Xamarin.forms.ios 上的共享按钮

swift - UIView 的圆角矩形的边框没有散布在边缘上。 Xamarin 操作系统

c# - 将项目转换为 Xamarin 统一 api : "Failed to load output manifest for actool for the file..."

ios - 带有自定义 View 的 Xamarin.Forms 弹出对话框

visual-studio - 如何在 Visual Studio 中的 NuGet 打包之前修改 .nuspec 文件

MySQL IO操作超时

xamarin - 从条目中删除填充