【发布时间】:2018-04-27 22:23:13
【问题描述】:
我创建了一个列表视图,其中包含背景绑定到 itemssource 的子视图。
这在调用 PropertyChanged 事件的 Android 上运行良好。
但不幸的是,它在 iOS 上并没有那么流畅,只有在我将元素滚动出视图并返回视图以重绘它之后,才会反映背景变化。
还有其他方法可以手动刷新内容吗?
代码:
<ListView x:Name="ListView" ItemsSource="{Binding ListSource}" RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<ContentView Padding="10" BackgroundColor="{Binding BackgroundColor}">
<Label Text="{Binding Name}" HorizontalOptions="Center" TextColor="White" />
</ContentView>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
处理itemclick改变背景并移除选中的item。
ListView.ItemTapped += async (s, e) =>
{
var list = ListSource;
var listItem = list.First(c => c.Id == ((ListItem)e.Item).Id);
listItem.Selected = !listItem.Selected;
SelectListSource = list;
ListView.SelectedItem = null;
};
模型中的代码:
public Boolean Selected
{
get
{
return _selected;
}
set
{
_selected = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor"));
}
}
public Color BackgroundColor
{
get
{
if (Selected)
return Color.Black;
else
return Color.Blue
}
}
【问题讨论】:
-
忘记提及我使用的代码来自:stackoverflow.com/questions/25885238/…
标签: c# xaml xamarin xamarin.forms