主详细信息页面加载菜单项MVVM

时间:2020-05-25 15:13:54

标签: xamarin.forms

我使用的是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));
                }
            }

2 个答案:

答案 0 :(得分:1)

您可以在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; }
    }

答案 1 :(得分: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时看起来一样。