metro app 进行缓存,

一些页面每次请求服务端的数据都是一样的,这样就可以请求一次,然后把数据缓存起来

在OnNavigatedFrom 添加如下的语句

由于不能对一个集合对象进行缓存,下面介绍如何缓存一个几个集合对象,以 ObservableCollection<Advertisement>,为例

要想缓存这个集合, 需要对Advertisement,每个属性 创建一个

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

Windows.Storage.ApplicationDataContainer bitimgcontainer =
  localSettings.CreateContainer("bitimg", Windows.Storage.ApplicationDataCreateDisposition.Always);

Windows.Storage.ApplicationDataContainer activeNamecontainer =
localSettings.CreateContainer("activeName", Windows.Storage.ApplicationDataCreateDisposition.Always);

然后将遍历每一个Advertisement 的属性 加入到对应的 container里面

for (int i = 0; i < Advertisements.Count; i++)
           {
               localSettings.Containers["bitimg"].Values[i.ToString()] = Advertisements[i].bigImage;
               localSettings.Containers["activeName"].Values[i.ToString()] = Advertisements[i].activeName;
           }

 

读取缓存需要在OnNavigatedTo 事件里面判断导航的类型

if (e.NavigationMode== NavigationMode.Back)
           {

               for (int i = 0; i < ApplicationData.Current.LocalSettings.Containers["bitimg"].Values.Count; i++)
               {
                   Advertisement pTempAdvertisement = new Advertisement();
                   pTempAdvertisement.bigImage = ApplicationData.Current.LocalSettings.Containers["bitimg"].Values[i.ToString()].ToString();
                   pTempAdvertisement.activeName = ApplicationData.Current.LocalSettings.Containers["activeName"].Values[i.ToString()].ToString();
                   Advertisements.Add(pTempAdvertisement);
               }
               return;
           }

你可能感兴趣的:(metro)