等待Shell,当前,GoToAsync花费太多时间

42fyovps  于 2023-05-18  发布在  Shell
关注(0)|答案(2)|浏览(277)

我正在使用MAUI开发一个项目,我在shell导航方面遇到了一些问题。在我的代码中有一行:

await Shell.Current.GoToAsync(nameof(DetailMenuPage));

这个代码的执行时间非常长。我有一些业务代码之前,它不需要时间相比导航.此外,我还有其他业务,这是在导航期间执行(在出现事件的命令中),也不需要时间。在导航之前和期间有完整的代码:

public async Task SelectItem(object selectedCategorie)
{
   if (selectedCategorie is CategorieMenu)
   {
      var cSelectedCategorie = selectedCategorie as CategorieMenu;
      var sousCategorie = await _serviceToSousCategorieMenu.GetByCategorieMenuId(new List<int> { cSelectedCategorie.CategorieMenuId });
      StaticContext.CategorieMenuName = cSelectedCategorie.Name;
      StaticContext.SousCategorieMenu = sousCategorie;
      var watch = new System.Diagnostics.Stopwatch();
      watch.Start();
      await Shell.Current.GoToAsync(nameof(DetailMenuPage));
      watch.Stop();
      Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
   }
}

这里watch.ElapsedMilliseconds需要4692 ms。

[RelayCommand]
public async Task Load()
{
   var watch = new System.Diagnostics.Stopwatch();
   watch.Start();
   var allItem = await _serviceMenuItem.GetBySousCategorieMenuId(StaticContext.SousCategorieMenu.Select(sc => sc.SousCategorieMenuId).ToList());
   if (allItem != null && allItem.Count > 0)
   {
      var groupedItem = allItem.GroupBy(u => u.SousCategorieMenuId).Select(grp => grp.ToList()).ToList();
      Title = StaticContext.CategorieMenuName;
      groupedItem.ForEach(c =>
      {
          Items.Add(new CategorieGroup
          (
             StaticContext.SousCategorieMenu.FirstOrDefault(sc => sc.SousCategorieMenuId == c.FirstOrDefault().SousCategorieMenuId).Name,
                    c
          ));
      });
   }
   watch.Stop();
   Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms");
}

这里watch.ElapsedMilliseconds需要168 ms。
是我错过了什么还是你有什么建议?
编辑:
DetailMenuPage的构造函数:

public DetailMenuPage(DetailMenuPageViewModel vm)
{
    InitializeComponent();
    BindingContext = vm;
}

DetailMenuPage的含量:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Byron.Page.DetailMenuPage"
             xmlns:ViewModels="clr-namespace:Byron.ViewModel"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:DataType="ViewModels:DetailMenuPageViewModel"
             xmlns:Models ="clr-namespace:Byron.Model.DTO"
             xmlns:Converter="clr-namespace:Byron.Helper.Converter"
             xmlns:IHM ="clr-namespace:Byron.Model.IHM"
             Routing.Route="DetailPage"
             Title="{Binding Title}">
    <ContentPage.Behaviors>
        <toolkit:EventToCommandBehavior EventName="Appearing" Command="{Binding LoadCommand}" />
    </ContentPage.Behaviors>
    <ContentPage.Resources>
        <Converter:DoubleToStringConverter x:Key="doubleToStringConverter" />
    </ContentPage.Resources>
    
    
    <ScrollView>
        <StackLayout>
            <CollectionView ItemsSource="{Binding Items}" IsGrouped="True" >
                <CollectionView.GroupHeaderTemplate>
                    <DataTemplate x:DataType="IHM:CategorieGroup">
                        <StackLayout HorizontalOptions="FillAndExpand">
                            <Label Text="{Binding GroupTitle}" Style="{StaticResource TitleGroupMenu}"/>
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.GroupHeaderTemplate>
                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="Models:MenuItem">
                        <Frame CornerRadius="10" Margin="10" BorderColor="{AppThemeBinding Light=Black, Dark=White}" BackgroundColor="Transparent">
                            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Name}" Style="{StaticResource ItemMenu}" />
                                    <Label Text="{Binding Prix, Converter={StaticResource doubleToStringConverter}}" HorizontalOptions="EndAndExpand" Style="{StaticResource ItemMenu}"/>
                                </StackLayout>
                                <Rectangle HeightRequest="1" BackgroundColor="Black" Margin="0,0,100,0" />
                                <Label Text="{Binding Description}" VerticalOptions="Center" TextColor="{AppThemeBinding Light=Black, Dark=White}" />
                            </StackLayout>
                        </Frame>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
    </ScrollView>
</ContentPage>

和视图模型的构造函数:

public DetailMenuPageViewModel(Services.SQLite.Interfaces.IServiceMenuItem serviceMenuItem)
 {
     _serviceMenuItem = serviceMenuItem;
 }
xesrikrc

xesrikrc1#

<ScrollView>
        <StackLayout>
            <CollectionView ItemsSource="{Binding Items}" IsGrouped="True" >

非垂直限制的视觉元素不应位于其他非垂直限制的容器内。
有趣的是,当人们抱怨性能时,我通常会看到上述两种组合。你的代码远不止于此。
限制您的VisualElements。无论是要求身高还是其他。
我想我可以安全地推荐编辑:

<Grid>
     <CollectionView...

(不知道你在做什么)

7kjnsjlb

7kjnsjlb2#

除了H.A.H.的答案,考虑这些变化:
1.你需要ScrollView吗?CollectionView知道如何滚动。如果去掉ScrollView,那么也要去掉StackLayout(或者从H.A.H.的答案中去掉Grid)。

<ScrollView>
    <StackLayout>
        <CollectionView

变成:

<CollectionView

1.无论在何处使用StackLayout,都应根据方向更改为VerticalStackLayoutHorizontalStackLayout
1.使用Border代替Frame。Frame是为了向后兼容Xamarin而保留的,但使用了旧的渲染方法。Frame文档建议使用边框。
1.您的Load方法在Appearing事件期间运行。在填充集合时,不要使用Items.Add。(我认为)这会触发每个Add上的布局逻辑。相反,在局部变量中构建集合,然后将其分配给Items

var items = new ...same-type-as-Items...();
...
items.Add(...);   // Add to local `items` instead of `Items`.
...
// When all items have been added.
Items = items;

1.考虑将HeightRequest设置在<CollectionView.ItemTemplate>内部。这个放在外面的布局上。除非您将FrameBorder放在框架/边框的内容上:<VerticalStackLayout HeightRequest="99" ...> .用看起来合适的高度替换“99”。固定高度的项目布局起来要快得多。

相关问题