2

我有一个标签页,它的 ItemSource 与 viewmodel 绑定以动态生成标签。下面是xaml的代码片段

<pages:ScrollableTabbedPage
x:Class="Client_to_Kitchen.CategorizedMenuPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Client_to_Kitchen"
xmlns:pages="clr-namespace:Client_to_Kitchen.Views;assembly=Client_to_Kitchen"
Title="Menu"
ItemsSource="{Binding Menu}">

<TabbedPage.BindingContext>
    <local:CategorizedMenuViewModel />
</TabbedPage.BindingContext>

<TabbedPage.ItemTemplate>
    <DataTemplate>
        <ContentPage Title="{Binding GroupName}" BackgroundColor="{StaticResource SecondaryColor}">
            <ListView
                x:Name="MyListView"
                CachingStrategy="RecycleElement"
                HasUnevenRows="True"
                IsPullToRefreshEnabled="True"
                IsRefreshing="{Binding IsLoading}"
                ItemTapped="Handle_ItemTapped"
                ItemsSource="{Binding MenuItems}"
                RefreshCommand="{Binding FoodMenuListCommand}"
                SeparatorColor="{StaticResource SecondaryLightColor}"
                SeparatorVisibility="None">

选项卡会生成,甚至列表视图也会按预期填充。只有 IsRefreshing 和 RefreshCommand 的绑定不起作用。我究竟做错了什么?

带有动态选项卡的页面截图:

在此处输入图像描述

以下是视图模型代码片段

public class CategorizedMenuViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool _isLoading;
    public bool IsLoading
    {
        get { return _isLoading; }
        set
        {
            _isLoading = value;
            OnPropertyChanged(nameof(IsLoading));
        }
    }

    private ObservableCollection<MenuListResponse> _menu;
    public ObservableCollection<MenuListResponse> Menu
    {
        get { return _menu; }
        set
        {
            _menu = value;
            OnPropertyChanged(nameof(Menu));
        }
    }

    public CategorizedMenuViewModel()
    {
        FoodMenuListCommand.Execute(null);
    }

    private ICommand _foodMenuListCommand;
    public ICommand FoodMenuListCommand => _foodMenuListCommand ?? (_foodMenuListCommand = new Command(async () => await GetMenuList()));
    public async Task GetMenuList()
    {
        IsLoading = true;
        int counter = 0;

        string[] listImages = new string[4];
        listImages[0] = "https://www.shareicon.net/data/512x512/2016/04/11/747949_animal_512x512.png";
        listImages[1] = "https://www.shareicon.net/data/512x512/2016/08/01/805257_cinema_512x512.png";
        listImages[2] = "https://www.shareicon.net/data/512x512/2016/08/19/817540_food_512x512.png";
        listImages[3] = "https://www.shareicon.net/data/512x512/2016/08/01/805240_food_512x512.png";
        try
        {
            MenuListStore _menuStoreObject = new MenuListStore();
            List<MenuListResponse> response = await _menuStoreObject.GetFoodMenuList();

            foreach (var items in response)
            {
                foreach (var item in items.MenuItems)
                {
                    if (counter > 3)
                        counter = 0;
                    item.Image = listImages[counter];
                    counter++;
                }
            }

            Menu = new ObservableCollection<MenuListResponse>(response);
        }
        catch (Exception ex)
        {
            await Application.Current.MainPage.DisplayAlert("Alert!", $"No internet", "Ok");
            IsLoading = false;
            return;
        }
        IsLoading = false;
    }

请注意:- 在单独的选项卡式页面中,我没有将选项卡式页面的 ItemSource 和 ContentPage Title 绑定到视图模型以生成动态选项卡,而是有 2 个带有 listview 的静态内容页面,RefreshCommand 绑定可以正常工作。

4

1 回答 1

2

我遇到了类似的问题,原来 List 位于 ScrollView 中,(我假设)它在到达 List 之前正在消耗下拉事件。删除 ScrollView 对其进行了排序 - 无论如何都不需要它。您的父控件是一个 ScrollableTabbedPage ,听起来它会遇到类似的问题。

于 2019-07-20T12:05:33.060 回答