c# - 如何在后面的代码中将图像嵌入到 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; }
}
解决方案
推荐阅读
- javascript - 信用问题计算
- react-native - 使用 null GraphQL 数据反应原生钩子
- amazon-web-services - 在 Amazon AWS Lightsail SSH 终端 vim 中退出“插入”模式
- reactjs - 将 react 与 redux 结合使用的最佳方法是什么?
- laravel - 在 Laravel 中,如何访问存储目录中的所有文件?
- javascript - 如何正确设置 CORS 以将带有 presignedUrl 的文件上传到 Firebase 存储?
- javascript - 如何从具有嵌套对象的对象中获取价值?
- javascript - Firebase 数据库 StartAt 规则
- webpack - 生产与开发中的Vuetify字体大小不同
- javascript - CSS表格样式 - 表格中心边框之间的间距