【问题标题】:ListView items get updated only after scrolled back into view - Xamarin FormsListView 项目仅在滚动回视图后才会更新 - Xamarin Forms
【发布时间】: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
        }
    }

【问题讨论】:

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

好的,所以我找到了使用 XFGloss 的解决方法。

在这个 nuget 的帮助下添加了对 ViewCell 的绑定。

<ViewCell xfg:CellGloss.BackgroundColor="{Binding BackgroundColor}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    相关资源
    最近更新 更多