簡體   English   中英

Xamarin Forms ListView 元素被禁用

[英]Xamarin Forms ListView elements are disabled

在我的應用程序中,我使用Shell tabs 選項卡之一是包含 ListView 的 ContentPage。

當我單擊 ListView 的任何元素時,我正在導航到另一個視圖,並且在導航回 ListView 元素后被禁用(參見底部鏈接中的圖像)。

我該如何解決?

查實事實:

  1. 錯誤僅發生在iOS上,應用程序在 Android 上正常工作
  2. ListView 的 Selected 元素是 null
  3. 該錯誤與傳遞對 ListView 的 ObservableCollection 元素的引用無關

我有 escaping 錯誤場景,但這不是問題的救贖:

當應用程序初始化時,單擊另一個選項卡並返回問題選項卡。

資源:

使用 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>

使用 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>

和 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));
    }
}
}

圖片:

普通列表視圖

損壞的列表視圖

我將 ListView 更改為 CollectionView 並且它有效!

不幸的是,我將不得不完全自己可視化 TextCell 元素。

謝謝大家的意見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM