【问题标题】:Master detail page Loading menu entries MVVM主详情页面 加载菜单条目 MVVM
【发布时间】:2020-05-26 15:06:08
【问题描述】:

我正在使用 MVVM,并且我有主详细信息,其中我有 9 个条目加上一些 if 语句以在用户登录与否时显示正确的条目。但是加载所有这些需要很长时间是否有其他方法可以加快进程?我正在使用 if 语句添加加载方法,因此您可以看到仅加载菜单条目确实需要一段时间。我还添加了选择方法

     public void LoadData()
            {
                MainMenuEntries = new ObservableCollection<MenuEntry>();

                if (LangUpLoggedUser.LoggedIn)
                {

                    MainMenuEntries.Add(new MenuEntry
                    {

                        Name = AppResources.A_StringCategoryUserProfile,
                        Icon = GrialIconsFont.User,
                        CreatePage = () => new UserProfile()

                    });
                }
                else if (LangUpLoggedUser.LoggedOffline)
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryUserProfile,
                        Icon = GrialIconsFont.User,
                        CreatePage = () => new UserProfile()

                    });

                }
                else
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryLoginSignUp,
                        Icon = GrialIconsFont.User,
                        CreatePage = () => new Login()
                    });

                }

                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategorySettings,
                    Icon = GrialIconsFont.Settings,
                    CreatePage = () => new Settings()

                });
                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryHelp,
                    Icon = GrialIconsFont.AlertInfo,
                    CreatePage = () => new Help()
                });
                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryCredits,
                    Icon = GrialIconsFont.Box,

                    CreatePage = () => new Credits()
                });
                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryPromo,
                    Icon = GrialIconsFont.Hashtag,
                    CreatePage = () => new Promo()
                });

                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryPickYourTheme,
                    HasSeparator = true,
                    Icon = GrialIconsFont.Fire,
                    CreatePage = () => new PickYourTheme()
                });

                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryArticlesForPurchase,
                    Icon = GrialIconsFont.ShoppingCart,
                    CreatePage = () => new TabMenuArticlesForPurchase()
                });
                if (LangUpLoggedUser.LoggedIn || LangUpLoggedUser.LoggedOffline)
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryMyArticles,
                        Icon = GrialIconsFont.File,
                        CreatePage = () => new TabMenuMyArticles()
                    });

                }

                else
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryMyArticles,
                        Icon = GrialIconsFont.File,
                        CreatePage = () => new Login()
                    });
                }
                if (LangUpLoggedUser.LoggedIn || LangUpLoggedUser.LoggedOffline)
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryDictionary,
                        Icon = GrialIconsFont.Book,
                        CreatePage = () => new Dictionary()
                    });
                }
                else
                {
                    MainMenuEntries.Add(new MenuEntry
                    {
                        Name = AppResources.A_StringCategoryDictionary,
                        Icon = GrialIconsFont.Book,
                        CreatePage = () => new Login()
                    });
                }

                MainMenuEntries.Add(new MenuEntry
                {
                    Name = AppResources.A_StringCategoryArticles,
                    Icon = GrialIconsFont.File,
                    CreatePage = () => new ArticleBrowser()
                });
            }

public MenuEntry MainMenuSelectedItem
        {
            get { return _selectedMainMenuEntry; }
            set
            {
                if (SetProperty(ref _selectedMainMenuEntry, value) && value != null)
                {
                    Page page;

                    if (value.PageType != null)
                    {
                        page = CreatePage(value.PageType);
                    }
                    else
                    {
                        page = value.CreatePage();
                    }

                    NavigationPage navigationPage;

                    if (value.NavigationPageType == null)
                    {
                        navigationPage = new NavigationPage(page);
                    }
                    else
                    {
                        navigationPage = (NavigationPage)Activator.CreateInstance(value.NavigationPageType, page);
                    }

                    if (value.UseTransparentNavBar)
                    {
                        GrialNavigationPage.SetIsBarTransparent(navigationPage, true);
                    }

                    if (_selectedMainMenuEntry.IsModal)
                    {
                        _navigation.PushModalAsync(navigationPage);
                    }
                    else
                    {
                        _openPageAsRoot(navigationPage);
                    }

                    _selectedMainMenuEntry = null;
                    NotifyPropertyChanged(nameof(MainMenuSelectedItem));
                }
            }

【问题讨论】:

  • 您为什么要创建所有页面的实例来填充菜单?当用户选择菜单选项时,创建页面会更有效率
  • 怎么做?你能分享一个例子吗
  • @Jason 好的,我这样做还是很慢,请问您还有其他建议吗?
  • 你需要做一些分析来找出具体是什么导致它变慢

标签: xamarin.forms


【解决方案1】:

不要在每次添加新的MenuEntry 时都创建新页面。

您可以关注document添加TargetType,并在需要时创建页面:

    MainMenuEntries.Add(new MenuEntry
    {

        Name = AppResources.A_StringCategoryUserProfile,
        Icon = GrialIconsFont.User,
        TargetType = typeof(UserProfile)
    });

    MainMenuEntries.Add(new MenuEntry
    {

        Name = AppResources.A_StringCategoryUserProfile,
        Icon = GrialIconsFont.User,
        TargetType = typeof(Login)
    });

在你的 viewModel 中:

  navigationPage = (NavigationPage)Activator.CreateInstance(value.TargetType);

Here 是您可以关注的示例项目。

在你的第一个 if-else 语句中:

LangUpLoggedUser.LoggedInLangUpLoggedUser.LoggedOffline 看起来一样。

【讨论】:

    【解决方案2】:

    您可以在 xaml 中创建一个绑定组件,并且一次只加载一次显示模型,而不是一次加载一个。该组件可以是一个列表视图。

    >

    <ListView ItemsSource="{Binding MenuItems}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <Label Text="{Binding Description}"/>
                                    <Button Command="{Binding Command}"/>
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    

    在 ViewModel 中,你添加这个(你可以添加一个范围):

    >

    //Property
    private List<MenuItem> menuItems
    
    public List<MenuItem> MenuItems { get; set; }    
        {
            get { return menuItems; }
            set
            {
                MenuItems = value;
                RaisePropertyChanged("MenuItems");
            }
        }
    
    MenuItems = new List<MenuItem>();
                MenuItems.Add(new MenuItem()
                {
                    Description = AppResources.MySafetyStartsHere,
                    Command = new DelegateCommand(() => { })
                });
    

    模型:

    >

    public class MenuItem
        {
            public string Description { get; set; }
    
            public ICommand Command { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多