【发布时间】:2020-07-29 03:40:21
【问题描述】:
我已经尝试在我的模型中使用 string 到 ImageSource,但我仍然没有在 ImageCell 中显示图像。
如果我尝试这行代码<Image Source="https://img.icons8.com/carbon-copy/2x/user.png"></Image>,图像显示正常。
型号
public class Contact
{
public string Name { get; set; }
public string Status { get; set; }
public ImageSource ImageUri { get; set; }
}
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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App1.ListPage">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="listView" SeparatorVisibility="Default" SeparatorColor="Blue">
<ListView.ItemTemplate>
<DataTemplate>
<!--<TextCell Text="{Binding Name}" Detail="{Binding Status}" TextColor="Accent" DetailColor="Beige"></TextCell>-->
<!--<TextCell Text="{Binding Name}" Detail="{Binding Status}"></TextCell>-->
`<ImageCell Text="{Binding Name}" Detail="{Binding Status}" ImageSource="{Binding ImageUri}`"></ImageCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!--<Image Source="https://img.icons8.com/carbon-copy/2x/user.png"></Image>-->
</StackLayout>
</ContentPage.Content>
</ContentPage>
XAML.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListPage : ContentPage
{
public ListPage()
{
InitializeComponent();
listView.ItemsSource = new List<Contact>
{
new Contact{ Name="Monkey D. Luffy", ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png"))},
new Contact{ Name="Monkey D. Dragon",
ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png"))},
new Contact{ Name="Portgas D. Ace",
ImageUri=ImageSource.FromUri(new Uri("https://img.icons8.com/carbon-copy/2x/user.png")), Status="Hello World!"}
};
}
}
【问题讨论】:
-
您可以在数据模板
ViewCell中直接使用Image而不是ImageCell。 -
你的图片尺寸是多少?我认为您需要在您的
ListView中添加HasUnevenRows="true",您的图像看起来就像被行高隐藏了 -
@cahyo ViewCell 有效,我不知道 ImageCell 有什么问题
-
我将我的 xamarin 表单 nuget 包更新为
4.7.0.1179,这是我的测试图像:aka.ms/campus.jpg,这个图像很大,但正在等待几秒钟。它可以正常工作。imgur.com/a/UovlCAs如果可以,您可以使用我的图片网址进行测试。
标签: c# xamarin xamarin.forms xamarin.android