下面的代码在 Android 上运行良好,我可以在 2 秒内看到 CollectionView 中填充的数据。但当我在 IOS 上尝试时,大约需要 60-65 秒。
当我在IOS上调试时,似乎问题不在于从数据库获取数据。只需要0.5秒。但之后,用该数据填充 CollectionView 并将其显示在 UI 上大约需要 1 分钟。但在Android上,没有问题。
注意:Android 在调试模式下在模拟器(Pixel 4 - API 33)上进行了测试, IOS 在我的本地 Iphone 11 设备上以热重载调试模式进行了测试, 框架:.Net 7.0, 来自数据库的数据计数:729 行
查看:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Hm.View.Dava_Dosyalari.GetFilesPage"
xmlns:model="clr-namespace:Hm.Model"
xmlns:view ="clr-namespace:Hm.View.Files"
xmlns:viewmodel="clr-namespace:Hm.ViewModel.Files"
x:DataType="viewmodel:FilesViewModel"
Title="Files">
<Grid RowDefinitions="40, *">
<Frame Grid.Row="0" BackgroundColor="AntiqueWhite" Padding="0">
<Grid ColumnDefinitions="*,Auto" ColumnSpacing="5">
<SearchBar Grid.Column="0" x:Name="searchBar" TextChanged="searchBar_TextChanged" TextColor="Black" VerticalOptions="Center"/>
<Label Grid.Column="1" VerticalOptions="Center" Text="{Binding TotalCount, StringFormat='Total: {0}'}" Margin="0,0,5,0"/>
</Grid>
</Frame>
<RefreshView Margin="10"
Grid.Row="1"
Command="{Binding GetFilesCommand}"
IsRefreshing="{Binding IsRefreshing}">
<ContentView>
<CollectionView ItemsSource="{Binding files}"
SelectionMode="None">
<CollectionView.EmptyView>
<StackLayout Padding="50" Spacing="10">
<Image Source="aa.png" Aspect="AspectFit"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<Label Text="Wait for getting files..."
Style="{StaticResource MediumLabel}"
HorizontalOptions="Center"/>
</StackLayout>
</CollectionView.EmptyView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Files">
<Grid Padding="10" HorizontalOptions="Fill">
<Frame HeightRequest="150" CornerRadius="20">
<Frame.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:FilesViewModel}}, Path=GoToFileCommand}"
CommandParameter="{Binding .}"/>
</Frame.GestureRecognizers>
<VerticalStackLayout VerticalOptions="Center" Spacing="2">
<Label FontAutoScalingEnabled="True" Style="{StaticResource SmallLabel}" Text="{Binding attr1, StringFormat='Attr1: {0}'}" Margin="3,0,0,0"/>
<Label FontAutoScalingEnabled="True" Style="{StaticResource SmallLabel}" Text="{Binding attr2, StringFormat='Attr2: {0}'}" Margin="3,0,0,0"/>
<Label FontAutoScalingEnabled="True" Style="{StaticResource MicroLabel}" Text="{Binding attr3, StringFormat='Attr3: {0}'}" Margin="3,0,0,0"/>
<Label FontAutoScalingEnabled="True" Style="{StaticResource MicroLabel}" Text="{Binding attr4, StringFormat='Attr4: {0}'}" Margin="3,0,0,0"/>
</VerticalStackLayout>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentView>
</RefreshView>
<ActivityIndicator
Grid.ColumnSpan="1"
Grid.RowSpan="2"
HorizontalOptions="Fill"
IsRunning="{Binding IsBusy}"
IsVisible="{Binding IsBusy}"
VerticalOptions="Center" />
</Grid>
视图模型:
public partial class FilesViewModel : BaseViewModel
{
[ObservableProperty]
bool isRefreshing;
FilesService fileService;
public ObservableCollection<Files> files { get; set; } = new();
public ObservableCollection<Files> _allfiles { get; set; } = new();
[ObservableProperty]
string totalCount;
public FilesViewModel(FileService service)
{
fileService = service;
GetFilesCommand.ExecuteAsync(null);
}
[RelayCommand]
async Task GetFilesAsync()
{
if (IsBusy)
return;
try
{
IsBusy = true;
var fileList = await fileService.GetFilesFromDb();
if (files != null || files.Count > 0)
files.Clear();
if (_allfiles != null || _allfiles.Count > 0)
_allfiles.Clear();
await App.Current.Dispatcher.DispatchAsync(() =>
{
fileList.ForEach(i => files.Add(i));
fileList.ForEach(i => _allfiles.Add(i));
TotalCount = files.Count.ToString();
});
}
catch (Exception ex)
{
await Shell.Current.DisplayAlert("Warning", ex.Message, "OK");
return;
}
finally
{
IsRefreshing = false;
IsBusy = false;
}
}
[RelayCommand]
async Task GoToFileAsync(Files files)
{
if(files== null)
return;
await Shell.Current.GoToAsync(nameof(GoToFilePage), true, new Dictionary<string, object>()
{
{"Att1", files },
{"Id", files.id }
});
}
}
到目前为止,我已经尝试从视图中删除 RefreshView,从 viewmodel GetFiles 命令中删除调度程序。但没有任何作用,在 IOS 上仍然很慢。
我的代码有问题还是这只是关于 .net maui?
谢谢。
尝试使用
ObervableRangeCollection
代替并使用它的 AddRange
方法。这允许您将多个项目添加到集合中,而无需为每个新项目触发 CollectionChanged
事件