首页 文章

只有在滚动回视图后才能更新ListView项目 - Xamarin Forms

提问于
浏览
0

我创建了一个listview,其中包含其背景绑定到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>

处理项目单击以更改背景并删除选定项目 .

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
        }
    }

1 回答

  • 0

    好的,我通过使用XFGloss找到了解决方法 .

    在此nuget的帮助下为ViewCell添加了绑定 .

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

相关问题