使用Windows Phone绑定和图像序列使事情变得正确时,我遇到了一些麻烦。假设我的平均评分在1-10之间,并且有5张图片(如星级评分系统)。现在,我要显示该图像的平均评分。每颗星的值为2,所以填充的5颗星的平均评级为10,填充的1.5颗星的平均评级为3。
我的XAML看起来像:
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Margin="150,-20,0,0" Source="/Images/icon_star.png" Stretch="None"/>
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Margin="166,-20,0,0" Source="/Images/icon_star.png" Stretch="None"/>
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Margin="182,-20,0,0" Source="/Images/icon_star_half.png" Stretch="None"/>
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Margin="198,-20,0,0" Source="/Images/icon_star_empty.png" Stretch="None"/>
<Image HorizontalAlignment="Left" VerticalAlignment="Top" Margin="214,-20,0,0" Source="/Images/icon_star_empty.png" Stretch="None"/>
这是我的模型:
public class Comment
{
public string comment { get; set; }
public int c_id { get; set; }
public double rating { get; set; }
public string created { get; set; }
}
在Android中,使用等级栏小部件或使用列表适配器填充图像很容易,如下所示:
Map<Integer, ImageView> vars = new HashMap<Integer, ImageView>();
vars.put(1, view.star_1);
vars.put(2, view.star_2);
vars.put(3, view.star_3);
vars.put(4, view.star_4);
vars.put(5, view.star_5);
while(i <= 5) {
if(i <= rate) {
ImageView star = vars.get(i);
star.setImageDrawable(getContext().getResources().getDrawable(R.drawable.icon_star));
} else {
if(rate % 2 != 0 && i == (int)(Math.round(rate))) {
ImageView star = vars.get(i);
star.setImageDrawable(getContext().getResources().getDrawable(R.drawable.icon_star_half));
} else {
ImageView star = vars.get(i);
star.setImageDrawable(getContext().getResources().getDrawable(R.drawable.icon_star_dis));
}
}
i++;
}
在iOS中,我使用了图像,IBOutletcollection和cellForRowAtIndexPath。
对于Windows,我使用绑定,但是我无法解决这个问题。
对于简单的绑定,此操作太复杂了。
您将要做的是创建一个“评分”用户控件。然后添加一个Value
依赖项属性。这意味着您可以执行以下操作:
<local:Rating Value="{Binding rating}"/>
控件本身将处理用户输入,将值限制为0.5的倍数,依此类推。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句