我注意到每次选择母版页菜单项时都会从头开始加载detailpage(因此每次调用它的构造函数)。 有没有办法实现一次加载的详细信息页面,所以每次选择菜单项都会显示/不显示详细页面?
谢谢, Lewix
答案 0 :(得分:0)
是的,你可以缓存页面。
这是一个存储页面的字典示例。
public partial class MyMasterDetailPage : MasterDetailPage
{
public MyMasterDetailPage()
{
Pages = new Dictionary<MenuType, Page>();
}
public enum MenuType
{
Home,
Settings,
Map
}
private Dictionary<MenuType, Page> Pages { get; set; }
public async Task NavigateAsync(MenuType id)
{
Page newPage;
if (!Pages.ContainsKey(id)) // check the page is already in the dictionary
{
Page page;
switch (id)
{
case MenuType.Home:
page = new ContentPage()
{
Title = "Home",
};
Pages.Add(id, page);
break;
case MenuType.Map:
page = new ContentPage()
{
Title = "Map",
};
Pages.Add(id, page);
break;
case MenuType.Settings:
page = new ContentPage()
{
Title = "Settings",
};
Pages.Add(id, page);
break;
}
}
newPage = Pages[id];
if (newPage == null)
return;
Detail = newPage; // assign the page
}
}
答案 1 :(得分:0)
我解决了在MasterDetailPage中实现字典页面缓存的问题:
// in constructor
MasterPageMenuCache = new Dictionary<Type, Page>();
// in OnItemSelected
if (MasterPageMenuCache.Count == 0)
MasterPageMenuCache.Add(typeof(FirstDefaultDetailPage), Detail);
var item = e.SelectedItem as MasterPageItem;
if (item != null)
{
if (MasterPageMenuCache.ContainsKey(item.TargetType))
{
Detail = MasterPageMenuCache[item.TargetType];
}
else
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
MasterPageMenuCache.Add(item.TargetType, Detail);
}
masterPage.ListView.SelectedItem = null;
IsPresented = false;
}