本文介绍了如何在Xamarin Forms MasterDetailPage项目中一次加载detailpage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我注意到每次选择主页页面菜单项时都会从头开始加载detailpage(因此每次都调用它的构造函数).有没有一种方法可以实现一次加载一次的详细信息页面,因此每次选择菜单项时,都会简单地显示/不显示详细信息页面?
I noticed detailpage is loaded from scratch everytime masterpage menu item is selected (so its constructor is called everytime).Is there a way to implement a detailpage which is loaded once, so everytime menu item is selected detailpage is simply showed/not showed?
谢谢,刘易斯
推荐答案
我解决了在MasterDetailPage中实现字典页面缓存的问题:
I solved implementing a dictionary page cache in 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;
}
这篇关于如何在Xamarin Forms MasterDetailPage项目中一次加载detailpage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!