Xamarin Forms ListView elements are disabled

Viewed 150

In my app I use Shell tabs. One of the tab is ContentPage containing ListView.

When I click on any of the elements of ListView I am navigating to another View, and after navigation back ListView elements are disabled (see images in the bottom links).

How can I fix it?

Checked facts:

  1. The bug only occurs on iOS, the application works correctly on Android
  2. ListView's Selected element is null
  3. The bug is not related to passing a reference to an element of ListView's ObservableCollection

I have escaping bug scenario, but it is not a problem salvation:

When app initialized click to another tab and return to problem tab back.

Source:

View with Shell (RayMainPage.xaml):

<?xml version="1.0" encoding="utf-8"?>

<Shell xmlns="http://xamarin.com/schemas/2014/forms"
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   xmlns:views="clr-namespace:RayHelper"
   x:Class="RayHelper.RayMainPage">
<TabBar>
    <Tab Title="Приюты"
         Icon="search">
        <ShellContent ContentTemplate="{DataTemplate views:HospiceListPage}" />
    </Tab>
    <Tab Title="Фонд Рэй"
         Icon="help">
        <ShellContent ContentTemplate="{DataTemplate views:RayProfilePage}" />
    </Tab>
    <Tab Title="Рейтинг"
         Icon="rank">
        <ShellContent ContentTemplate="{DataTemplate views:UsersRankPage}" />
    </Tab>
    <Tab Title="Пользователь"
         Icon="profile">
        <ShellContent ContentTemplate="{DataTemplate views:UserProfilePage}" />
    </Tab>
</TabBar>
</Shell>

View with ListView (HospiceListPage.xaml):

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="RayHelper.HospiceListPage"
         Title="Список приютов">
<ContentPage.Resources>
    ...
</ContentPage.Resources>

<StackLayout>
    <ListView x:Name="HospiceList"
              ItemsSource="{Binding Hospices}"
              SelectionMode="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell
                    Text="{Binding Name}"
                    Detail="{Binding Address}"
                    Command="{Binding Source={x:Reference Name=HospiceList}, Path=BindingContext.OpenHospiceProfileCommand}"
                    CommandParameter="{Binding Source={RelativeSource Self}, Path=BindingContext}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

</ContentPage>

and ViewModel for View with ListView (HospiceListPageViewModel.cs)

usings ...

namespace RayHelper.ViewModels
{
public class HospiceListPageViewModel : MainViewModel
{
    private ObservableCollection<Hospice> _hospices;
    public ObservableCollection<Hospice> Hospices
    {
        get => _hospices;
        set => SetProperty(ref _hospices, value);
    }
    
    public IMvxAsyncCommand<Hospice> OpenHospiceProfileCommand { get; }

    public HospiceListPageViewModel()
    {
        OpenHospiceProfileCommand = new MvxAsyncCommand<Hospice>(OpenHospiceProfileAsync);
        Hospices = new ObservableCollection<Hospice>();
        LoadData();
    }

    private async void LoadData()
    {
        var hospices = await GetHospices();
        foreach (var hospice in hospices)
        {
            Hospices.Add(hospice);
        }
    }

    protected override string ClassName => nameof(HospiceListPageViewModel);

    private async Task<IEnumerable<Hospice>> GetHospices()
    {
        try
        {
            var hospices = await RayHelperClient.GetHospices().ConfigureAwait(false);
            hospices.Sort();
            return hospices;
        }
        catch (Exception e)
        {
            ...
        }
        return Enumerable.Empty<Hospice>();
    }

    private async Task OpenHospiceProfileAsync(Hospice hospice)
    {
        await Navigation.PushAsync(new HospiceProfilePage(hospice));
    }
}
}

Images:

Normal list view

Broken list view

1 Answers

I think something is wrong with ListView. I changed the ListView to the CollectionView and the bug is gone.

Unfortunately I can't use the TextCell element with CollectionView. I will have to create a custom TextCell element entirely on my own.

Related