简体   繁体   English

Xamarin Forms ListView 元素被禁用

[英]Xamarin Forms ListView elements are disabled

In my app I use Shell tabs .在我的应用程序中,我使用Shell tabs One of the tab is ContentPage containing ListView.选项卡之一是包含 ListView 的 ContentPage。

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).当我单击 ListView 的任何元素时,我正在导航到另一个视图,并且在导航回 ListView 元素后被禁用(参见底部链接中的图像)。

How can I fix it?我该如何解决?

Checked facts:查实事实:

  1. The bug only occurs on iOS , the application works correctly on Android错误仅发生在iOS上,应用程序在 Android 上正常工作
  2. ListView's Selected element is null ListView 的 Selected 元素是 null
  3. The bug is not related to passing a reference to an element of ListView's ObservableCollection该错误与传递对 ListView 的 ObservableCollection 元素的引用无关

I have escaping bug scenario, but it is not a problem salvation:我有 escaping 错误场景,但这不是问题的救赎:

When app initialized click to another tab and return to problem tab back.当应用程序初始化时,单击另一个选项卡并返回问题选项卡。

Source:资源:

View with Shell (RayMainPage.xaml):使用 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):使用 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)和 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损坏的列表视图

I change the ListView to the CollectionView and It's Works!我将 ListView 更改为 CollectionView 并且它有效!

Unfortunately I will have to visualize the TextCell element entirely on my own.不幸的是,我将不得不完全自己可视化 TextCell 元素。

Thanks everybody for comments.谢谢大家的意见。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM