刷新命令在Listview中不起作用,Listview位于Xamarin.Forms的选项卡页面内

时间:2018-05-20 06:51:42

标签: c# listview xamarin.forms viewmodel tabbedpage

我有一个标签页,其中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的绑定不起作用。我做错了什么?

包含动态标签页面的屏幕截图:

enter image description here

以下是viewmodel代码的片段

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绑定到viewmodel以生成动态选项卡,而是有2个带listview的静态内容页面,RefreshCommand绑定正常工作。

1 个答案:

答案 0 :(得分:1)

我有一个类似的问题,结果发现列表位于ScrollView中(我认为)在到达列表之前正在消耗下拉事件。删除ScrollView即可对其进行排序-仍然不需要它。您的父控件是ScrollableTabbedPage,听起来像会遇到类似的问题。