【发布时间】:2021-01-26 20:57:21
【问题描述】:
你好,如果我违反了任何习俗,我很抱歉,这是我第一次在这里问。
我正在尝试创建一个 ListView,其 ItemSource 为 ObservableCollection<Opinion> 并由 ImageCell 组成。
Opinion 是一个类,其方法确定可绘制对象中的五张图片中的哪一张成为所述图像的来源。
这是意见类代码:
public class Opinion
{
public string User { get; set; }
public string Declaration { get; set; }
public int Rating { get; set; }
public string RatingSource;
public Opinion() { }
public Opinion(string user, string declaration, int rating)
{
this.User = user;
this.Declaration = declaration;
this.Rating = rating;
this.RatingSource = getRating(rating);
}
private string getRating(int rating)
{
switch (rating)
{
case 1:
return ("OneStar.png");
case 2:
return ("TwoStar.png");
case 3:
return ("ThreeStar.png");
case 4:
return ("FourStar.png");
case 5:
return ("FiveStar.png");
default:
return ("");
}
}
}
}
ListView 的 XAML:
<ContentPage Title="Oponions">
<StackLayout>
<ListView x:Name="OpinionsView" ItemsSource="{Binding opi}" HasUnevenRows="True" IsEnabled="False">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell ImageSource="{Binding RatingSource}"
Text="{Binding Declaration}"
TextColor="AntiqueWhite"
Detail="{Binding User}"
DetailColor="LightGray" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
以及它的 C# 代码:
namespace Interfaces23
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DeepArticle : TabbedPage
{
ObservableCollection<Opinion> opi { get; set; } = new ObservableCollection<Opinion>();
public DeepArticle(Article articulo)
{
InitializeComponent();
opi = articulo.Opinions;
OpinionsView.ItemsSource = opi;
lbDescription.Text = articulo.Description;
lbPrecio.Text = "Precio: " + articulo.Price;
Image img = new Image();
lbPrueba.Text = articulo.Opinions[0].RatingSource;
}
}
}
每个 Binding 都可以正常工作并正确显示信息,但图像除外。据我了解,RatingSource 应将“FiveStar.png”作为字符串返回,然后该字符串应作为 ImageCell 内 ImageSource 的值,但我必须遗漏一些东西。
【问题讨论】: