简体   繁体   English

Xamarin Forms 即使设置了绑定并将数据添加到可观察的集合中,集合视图也不会在 UI 上显示任何内容

[英]Xamarin Forms Collectionview not showing anything on UI even when binding is set and data is added to observable collection

I am quite a beginner so take it easy on me.我是一个初学者,所以请放轻松。 I have a collection view which is bound to an observable collection.我有一个绑定到可观察集合的集合视图。 The observable collection receives data and has the items but the Collectionview doesn't display anything at all.可观察集合接收数据并具有项目,但 Collectionview 根本不显示任何内容。 Could someone please help me with this.有人可以帮我解决这个问题。 Thanks.谢谢。

XAML XAML

        <CollectionView Grid.Row="1" ItemsSource="{Binding Fav}" x:Name="CVWatchItems" SelectionMode="Single" SelectionChanged="CVWatchItems_SelectionChangedAsync">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="8, 8, 8, 0">
                        <Frame BorderColor="LightGray" CornerRadius="0" HasShadow="True" Padding="5">
                            <Grid Padding="0" ColumnSpacing="0" RowSpacing="0" Margin="2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="120"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <ffimageloading:CachedImage Source="{Binding SingleimageUrl}" Aspect="AspectFill" WidthRequest="120" HeightRequest="100" Grid.Row="0" Grid.Column="0"/>

                                <Grid Grid.Row="0" Grid.Column="1" Padding="5">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Label Padding="0, 0, 0, 3" Text="{Binding Title}" LineBreakMode="TailTruncation" TextColor="Black" FontSize="Medium" Grid.Row="0"/>
                                    <Label Text="{Binding Price, StringFormat='Nu.{0}'}" FontSize="Small" TextColor="Black"  Grid.Row="1"  HorizontalOptions="StartAndExpand" />
                                    <Grid Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="7*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>

                                        <Label Text="{Binding location}" FontSize="Small" TextColor="Gray" Grid.Column="0" HorizontalOptions="StartAndExpand" VerticalOptions="EndAndExpand"/>
                                        <!--Watchlist Icon-->
                                        <Image Source="{Binding Favorite}" Grid.Column="1" Aspect="AspectFill" HeightRequest="28" Margin="2, 0" HorizontalOptions="EndAndExpand" VerticalOptions="EndAndExpand">
                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                                                <!--Give ID to each ad and stored data is retrieved through id.-->
                                            </Image.GestureRecognizers>
                                        </Image>
                                    </Grid>
                                </Grid>

                            </Grid>
                        </Frame>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

Code behind背后的代码

   public static ObservableCollection<Item> Fav { get; set; } = new ObservableCollection<Item>();
   public Watchlist ()
    {
        InitializeComponent ();
        NavigationPage.SetHasNavigationBar(this, false);
        NavigationPage.SetHasBackButton(this, false);

        CVWatchItems.SetBinding(CollectionView.ItemsSourceProperty, nameof(Fav));

        GetFavItems();
    }  

    private async void GetFavItems()
    {
        var MyFavorites = await Task.WhenAll(FirebaseDataHelper.GetFavoriteItems(allFavorites));
        
                foreach (var favorite in MyFavorites)
                {
                    if (favorite.Count > 0)
                    {
                        for (int i = 0; i < favorite.Count; i++)
                        {
                            favorite[i].IsFavorite = true;
                            if (!Fav.Any(s => s.Id == favorite[i].Id))
                                Fav.Add(favorite[i]);
                                
                        }
                    }
                }

               
      }

Thanks guys.多谢你们。

First your observable collection is static and you never set BindingContext to your page.首先,您的可观察集合是 static 并且您从未将 BindingContext 设置为您的页面。

public static ObservableCollection<Item> Fav { get; set; } = new ObservableCollection<Item>();

Remove static keyword from the definition of Fav, change binding in your XAML (`assign a Name to your page) like:从 Fav 的定义中删除 static 关键字,更改 XAML 中的绑定(`为您的页面分配名称),如:

<ContentPage x:Name="Root" ..../>

<CollectionView Grid.Row="1" ItemsSource="{Binding Source={x:Reference Name=Root}, Path=Fav}" ..../>

Also remove binding from your code behind.还要从后面的代码中删除绑定。 Last thing, your page has to implement INotifyPropertyChanged interface because you have to tell the page that this collection changed (your collection is filled after your async task is finished) So you have to raise PropertyChanged after your collection is filled.最后一件事,你的页面必须实现 INotifyPropertyChanged 接口,因为你必须告诉页面这个集合发生了变化(你的集合在你的异步任务完成后被填充)所以你必须在你的集合被填充后引发PropertyChanged

But maybe in your case is better and easier to set ItemsSource without binding because you are not using MVVM .但也许在您的情况下,不使用绑定设置 ItemsSource 会更好更容易,因为您没有使用MVVM If you are in code behind of your page it is not necessary to use binding.如果您在页面后面的代码中,则无需使用绑定。 You can set collectionview itemsource like:您可以设置collectionview itemsource,如:

 private async void GetFavItems()
    {
        var MyFavorites = await Task.WhenAll(FirebaseDataHelper.GetFavoriteItems(allFavorites));
        
                foreach (var favorite in MyFavorites)
                {
                    if (favorite.Count > 0)
                    {
                        for (int i = 0; i < favorite.Count; i++)
                        {
                            favorite[i].IsFavorite = true;
                            if (!Fav.Any(s => s.Id == favorite[i].Id))
                                Fav.Add(favorite[i]);
                                
                        }
                    }
                }

               CVWatchItems.ItemsSource = Fav;
      }

If you will use MVVM in your application you should use bindings.如果您将在应用程序中使用 MVVM,您应该使用绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Forms 更新可观察集合时集合视图未更新 - Xamarin Forms Collectionview not updating when observable collection is updated ListView不显示有关可观察的集合数据绑定的任何信息 - ListView Not showing anything about observable collection data binding 未将字符串添加到Observable Xamarin表单中 - String not Added to Observable Collection Xamarin Forms 将数据作为 DataTable 处理为 xamarin 形式的 Observable Collection - Deal with data as DataTable to Observable Collection in xamarin forms 在 Xamarin Forms 中将 Observable 集合绑定到我的 ListView 时遇到问题 - Having trouble binding an Observable collection to my ListView in Xamarin Forms 数据在 Collectionview 外的 Viewmodel 内绑定 Xamarin 不起作用,ui 未更新 - data Binding inside Viewmodel outside Collectionview Xamarin is not working, ui is not updating 订购Xamarin表格可观察的集合 - Ordering xamarin forms observable collection 即使绑定为真,Xamarin 表单活动指示器也不显示在按钮单击上 - Xamarin forms Activity Indicator Not Showing on Button click Even when Binding is true Xamarin forms 集合查看传递数据 - Xamarin forms CollectionView passing data Xamarin Forms CollectionView 绑定后保持为空 - Xamarin Forms CollectionView stays empty after binding it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM