首页 > 解决方案 > 如何在后面的代码中将图像嵌入到 ListView 中?

问题描述

我在 XAML 中创建了一个 ListView,并绑定到后面的 C# 代码中的列表。

我正在尝试从 C# 将嵌入图像插入到 ListView 中,但不确定我在这里是如何失败的。

图像属性设置为EmbeddedResource

这是我的 XAML

 <ListView x:Name="listView" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="5">
                            <Image Source="{Binding ImageUrl}"/>
                            <StackLayout HorizontalOptions="StartAndExpand">
                                <Label Text="{Binding Name}"/>
                                <Label Text="{Binding Status}" TextColor="Gray"/>
                            </StackLayout>
                            <Button Text="Follow"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

这是我的 C# 代码

public ListPage()
        {
            InitializeComponent();

            Image embeddedImage = new Image
            {
                Source = ImageSource.FromResource("App2.Images.image.jpg")
            };

            listView.ItemsSource = new List<Contact>
            {
                new Contact {Name = "Name", Status = "Hi!", ImageUrl = new Image{ Source = ImageSource.FromResource("App2.Images.cat.jpg")} },
                new Contact {Name = "John", Status = "Bye!" },
                new Contact {Name = "Jennifer"}
            };
        }

这是我的联系人模型

public class Contact
    {
        public string Name { get; set; }
        public string Status { get; set; }
        public Image ImageUrl { get; set; }
    }

标签: c#xamarinxamarin.formsxamarin.forms.listview

解决方案


推荐阅读