例如,我的应用程序中有3页。它有以下导航地图:
MainPage >> InfoPage >> SettingsPage.
因此,如果我从SettingsPage转到InfoPage,然后使用Back按钮,InfoPage将保持其状态。
但是,如果我使用后退按钮从InfoPage到MainPage,然后再转到InfoPage,那么InfoPage就会失去状态,并再次开始加载。
如何在应用程序运行期间始终保持InfoPage状态?我只需要初始化一次。
发布于 2014-06-11 04:24:28
处理此问题的最佳方法是为该页面创建一个缓存。然后在加载时从隔离存储中检索缓存。
你的流量会像这样
> >>OnNavigatedToMethod_AnyPage
> -->Check if cache exists in isolated storage
> --->If it does get the cache and load the values into the page
> ---> if it doesnt exist create a new one and save default values
>
> >>OnNavigatedFromMethod_AnyPage
> -->Load values into cache object
> --->Save cache object to isolated storage
那么我们是怎么做到的呢?
第一件事是Iso
下一步是实现它。
首先创建页面缓存对象
[DataContractAttribute]//This tells the EZ_Iso dll that this object is serializable
public PageOneCache{
[DataMember] //This tells the serializer to serialize this member
public bool flag1 {get; set;}
[DataMember]
public List<int> ages {get;set;}
public int boxes {get; set;} // This member doesn't have the [DataMember] so it wont get saved
}
好的,现在我们有了缓存对象,让我们保存它。
PageOneCache pageOneCache = new PageOneCache(){ flag1 = true, ages = new List<int>(){1,3,4}, boxes = 2};
if(EZ_iso.IsolatedStorageAccess.FileExists("pageOneCache")
Ez_iso.IsolatedStorageAccess.OverwriteFile("pageOneCache",pageOneCache);
else
Ez_iso.IsolatedStorageAccess.SaveFile("pageOneCache",pageOneCache);
一旦你这样做了,你的缓存被保存到电话的隔离存储。无论应用程序运行与否,它都是安全的。电话可以完全关机,一切都会好的。
现在用于检索
PageOneCache pageOneCache = (PageOneCache)EZ_iso.IsolatedStorageAccess.GetFile("pageOneCache",typeof(PageOneCache));
就这样!
https://stackoverflow.com/questions/24160768
复制相似问题