Xamarin.forms 母版详细信息页面加载菜单项MVVM
我使用的是MVVM,我有主详细信息,其中有9个条目和一些if语句,用于显示用户是否登录的正确条目。但是加载所有这些都需要很长时间。是否有其他方法可以加快进程?我添加了带有if语句的加载方法,这样您就可以看到只加载菜单项确实需要一段时间。我还添加了选择方法Xamarin.forms 母版详细信息页面加载菜单项MVVM,xamarin.forms,Xamarin.forms,我使用的是MVVM,我有主详细信息,其中有9个条目和一些if语句,用于显示用户是否登录的正确条目。但是加载所有这些都需要很长时间。是否有其他方法可以加快进程?我添加了带有if语句的加载方法,这样您就可以看到只加载菜单项确实需要一段时间。我还添加了选择方法 public void LoadData() { MainMenuEntries = new ObservableCollection<MenuEntry>();
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));
}
}
public void LoadData()
{
mainmenuteries=新的ObservableCollection();
if(LangUpLoggedUser.LoggedIn)
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryUserProfile,
Icon=GrialIconsFont.User,
CreatePage=()=>new UserProfile()
});
}
else if(LangUpLoggedUser.LoggedOffline)
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryUserProfile,
Icon=GrialIconsFont.User,
CreatePage=()=>new UserProfile()
});
}
其他的
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryLoginSignUp,
Icon=GrialIconsFont.User,
CreatePage=()=>新登录()
});
}
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategorySettings,
Icon=GRIALICONSONT.Settings,
CreatePage=()=>新设置()
});
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryHelp,
Icon=GrialIconsFont.AlertInfo,
CreatePage=()=>新帮助()
});
主菜单项。添加(新菜单项)
{
名称=AppResources.A\u StringCategoryCredits,
Icon=GrialIconsFont.Box,
CreatePage=()=>new Credits()
});
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryPromo,
Icon=GrialIconsFont.Hashtag,
CreatePage=()=>new Promo()
});
主菜单项。添加(新菜单项)
{
Name=AppResources.A_StringCategoryPickYourTheme,
HasSeparator=true,
Icon=GrialIconsFont.Fire,
CreatePage=()=>新建PickYourTheme()
});
主菜单项。添加(新菜单项)
{
名称=AppResources.A\u StringCategoryArticles供购买,
Icon=GrialIconsFont.ShoppingCart,
CreatePage=()=>新选项卡menuarticlesforpurchase()
});
if(LangUpLoggedUser.LoggedIn | | LangUpLoggedUser.LoggedOffline)
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryArticles,
Icon=GrialIconsFont.File,
CreatePage=()=>新的TabMenuMyArticles()
});
}
其他的
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryArticles,
Icon=GrialIconsFont.File,
CreatePage=()=>新登录()
});
}
if(LangUpLoggedUser.LoggedIn | | LangUpLoggedUser.LoggedOffline)
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryDictionary,
Icon=GrialIconsFont.Book,
CreatePage=()=>new Dictionary()
});
}
其他的
{
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryDictionary,
Icon=GrialIconsFont.Book,
CreatePage=()=>新登录()
});
}
主菜单项。添加(新菜单项)
{
名称=AppResources.A_StringCategoryArticles,
Icon=GrialIconsFont.File,
CreatePage=()=>new ArticleBrowser()
});
}
公共菜单主菜单选择编辑项
{
获取{return\u selectedMainMenutry;}
设置
{
if(SetProperty(ref _selectedMainMenutry,value)&&value!=null)
{
页码;
if(value.PageType!=null)
{
page=CreatePage(value.PageType);
}
其他的
{
page=value.CreatePage();
}
导航页面导航页面;
if(value.NavigationPageType==null)
{
navigationPage=新的navigationPage(第页);
}
其他的
{
navigationPage=(navigationPage)Activator.CreateInstance(value.NavigationPageType,page);
}
如果(value.UseTran)
<ListView ItemsSource="{Binding MenuItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Description}"/>
<Button Command="{Binding Command}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//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; }
}
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)
});
navigationPage = (NavigationPage)Activator.CreateInstance(value.TargetType);