XAML Xamarin正在移除CollectionView子系

voase2hg  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(136)

我在Xamarin的CollectionView中遇到了一个不刷新GUI元素的问题。
绑定到CollectionViewObservableCollection项目RefreshingEvent 期间被清除并填充新元素,但在屏幕上显示旧的StackLayout
原因是SingleItem控件未在Items.Clear()上移除/删除-如何执行

<CollectionView x:Name="collection"
       ItemsSource="{Binding Items}"
       SelectionMode="None">
       <CollectionView.ItemTemplate>
            <DataTemplate>
                  <StackLayout>
                       <controls:SingleItem/>
                       <Grid BackgroundColor="White" HeightRequest="5" HorizontalOptions="Fill" VerticalOptions="End"/>
                  </StackLayout>
            </DataTemplate>
       </CollectionView.ItemTemplate>
</CollectionView>

C# -没有什么“不寻常”的,这工作正常(在断点):

void RefreshCommand()
{
    List<Item> items = null;
    items = GetItems();
    if (items != null)
    {
        Items.Clear();
        foreach (var item in items)
            Items.Add(item);
    }
}

等待,在你前进,因为下面我写了我的“想法”如何解决它。
问题是我没有找到GUI AllChildren的“Clear”或“Remove”方法-Element的内部成员:

void RefreshCommand(Action clearCollectionView)
{
    List<Item> items = null;
    items = GetItems();
    if (items != null)
    {
        Items.Clear();
        cleacCollectionView.Invoke();
        foreach (var item in items)
            Items.Add(item);
    }
}
// page.xaml.cs:
...
        BindingContext = new Model(() => ???);
...
6psbrbz9

6psbrbz91#

您可以像这样编写Grid,并将ObservableCollection数据绑定到标签,而不是使用<controls:SingleItem/>

<CollectionView  x:Name="Collection"  ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid BackgroundColor="White" HeightRequest="5" HorizontalOptions="Fill" VerticalOptions="End">
                       
                        <Label
                                Text="{Binding itemName}"
                                FontAttributes="Bold"
                                x:Name="labelname" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
                       
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

相关问题