TabbedPage中的CollectionView不显示数据Xamarin.Forms

问题描述

这是背景。我有一个TabbedPage视图,其中包含3个ContentPage类型的Views。中间选项卡是一个内容页面,该页面具有一个CollectionView,该View绑定到相应viewmodel中的ObservableCollection。

这是问题所在。当我单击中间选项卡时,CollectionView中没有显示任何数据。我使用断点运行调试,可以看到在实例化Main TabbedPage时已填充了基础的ObservableCollection。选项卡本身中没有显示数据。

If I hit 'Save' on the View in VS,the simulator then refreshes and correctly shows the data.  What am I missing?  Should I be calling some sort of refresh method when tabs are clicked?  Just started working on Xamarin.Forms and teaching myself,so please be gentle. 

编辑:对不起,下面添加代码:我确实看到显示EmptyView字符串,直到我回到VS并在视图上单击“保存”。

这是我的观点:

<RefreshView IsRefreshing="{Binding IsRefreshing}" Command="{Binding RefreshCategoriesCommand}">  
            <CollectionView ItemsSource="{Binding ProductCategories}" 
                            EmptyView="No products found"
                            SelectionMode="Single" SelectedItem="{Binding SelectedCategory}" SelectionChangedCommand="{Binding GetCategoryDetailCommand}" 
                            ItemsLayout="VerticalGrid,2" >
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid Padding="15" Margin="0,10">
                            <Grid.RowDeFinitions>
                                <RowDeFinition Height="35" />
                                <RowDeFinition Height="35" />
                            </Grid.RowDeFinitions>
                            <Grid.ColumnDeFinitions>
                                <ColumnDeFinition Width="70" />
                                <ColumnDeFinition Width="80" />
                            </Grid.ColumnDeFinitions>
                            <Image Grid.RowSpan="2"
                           Source="{Binding image.src}"
                           Aspect="AspectFill"
                           HeightRequest="60"
                           WidthRequest="60" />
                            <Label Grid.Column="1"
                           Text="{Binding name}"
                           FontAttributes="Bold" 
                           LineBreakMode="TailTruncation"
                           VerticalOptions="End" />
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </RefreshView> 

viewmodel代码

 public CategoryGroupviewmodel(IDialogService dialogService,INavigationService navigationService,IConnectionService connectionService,IWooCommerceRepository wooCommerceRepository)
        : base(dialogService,navigationService,connectionService)
    {
        _wooCommerceRepository = wooCommerceRepository;
        GetCategoryDetailCommand = new Command(OnGetCategoryDetail);
        RefreshCategoriesCommand = new Command(OnRefreshCategories);

        GetCategories();
    }

 private async void GetCategories()
    {
        try
        {
            var categories = await _wooCommerceRepository.GetProductCategories();
            ProductCategories = new ObservableCollection<ProductCategory>((IEnumerable<ProductCategory>)categories);
        } catch (Exception ex)
        {
            await _dialogService.ShowDialog("Oops we hit an error " + ex.Message + "  " + ex.ToString(),"Error","OK");
        }

    }

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)