2

Is there a way to check if page/frame in Windows 8 application exists in cache? Let's assume I have two pages: Home and Clients (navigation cache enabled). When I navigate to clients from home (by button) clients are loaded from database in OnNavigatedTo method. I navigate back to Home by Back button and than again to Clients. Now I see that clients are loaded from cache, which is good, but than again from OnNavigatedTo method. I'd like to load clients from database only once, when I open page for the first time. Later just load clients from cache.

How can I check than if clients were previously loaded or load them only on first page load? Maybe some other method?

Thank you!

ignacy130
  • 322
  • 1
  • 2
  • 17
  • Assuming you are talking about webpages. From what I know from developing PHP based websites is that you specify whether the page may be cached, and if so, when it expires. So in general you are asking what page headers to set? Maybe this might be of interest to you: http://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php – Mike de Klerk Oct 01 '13 at 10:07
  • BTW, What has Windows 8 to do with anything? You tagged it with C#, so a .Net framework would be more appropriate, but that doesn't go into the title. Your question now suggests you are trying out a platform specific (Windows 8) feature/hack of accessing cached pages... (At Least to me...) EDIT: Oh I see now, I was thinking `ASP C#`. I have no understanding of Windows 8 Apps :) – Mike de Klerk Oct 01 '13 at 10:08
  • Oh, sorry no I'm not talking about web pages. My question is about Windows 8 app. – ignacy130 Oct 01 '13 at 10:08
  • I was gonna suggest kernel tag, what kind of question is this? – Behrooz Oct 01 '13 at 10:10
  • +1 I did Face this :) Ill share a work around – Anobik Oct 01 '13 at 10:11

2 Answers2

1

Here's a solution to it ...

Sinche no one wants to load from cache in metro app so it's always better to reset the cache size for the respective frame. For pages where you want it to load from the cache. Just keep an if loop. and also check for the forwardStack in the History object.

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);
    if (e.NavigationMode == NavigationMode.Back)
    {
        ResetPageCache();
    }
}

private void ResetPageCache()
{
    var cacheSize = ((Frame) Parent).CacheSize;
    ((Frame) Parent).CacheSize = 0;
    ((Frame) Parent).CacheSize = cacheSize;
}

Here's a blog. Ill recommend you go through this first :)

BLOG

EDIT---------------------

Here are two links. Since i never faced the situation of loading the file from previous cache so I can share some findings :)

Here's the accesscache class

Access cache

and here's for winJS

winJS cache

the access cache might meet your requirement if you set caching to true for the pages you want :)

Anobik
  • 4,841
  • 2
  • 17
  • 32
  • 1
    I would definitely like to load my page from cache. I'll correct my question. – ignacy130 Oct 01 '13 at 10:17
  • in that case just have `NavigationCacheMode = NavigationCacheMode.Enabled;` . If the page is there in the cache it will automatically load it from there. – Anobik Oct 01 '13 at 10:19
  • But this will happen in app. Ill share some thing more specific in an EDIt :) – Anobik Oct 01 '13 at 10:21
  • Yeah, it does work but OnNavigatedTo method invokes every time I open this page. I want to load clients from database at first page opening and later get them from cache. That's why I want to know whether clients are in cache or was it the first time page is opened. – ignacy130 Oct 01 '13 at 10:22
  • I guess you are on winJS ? or metro C# – Anobik Oct 01 '13 at 10:26
  • I'm on C#, as tagged. – ignacy130 Oct 01 '13 at 10:29
  • I did edit the answer. My part in the project was preventing the caching. you can go through the links :) I guess it might work out . – Anobik Oct 01 '13 at 10:30
  • I have doubts whether it is about Storage. Cache of objects displayed in GridView seems to me more like a global variable than exact files or folders. I'll try with flags. – ignacy130 Oct 01 '13 at 11:08
  • Ok, I solved my problem. Important is to have NavigationCacheMode enabled. In OnNavigatedTo method I just check whether list which to I saved data from database contains any elements. At first page opening it is empty so I load data from database to my list. Thus enabling NavigationCacheMode data in variables are stored in cache, and loaded while navigating to this page. Some flags may be required. I'll post it as answer in 6 hours. Can't do it now because my reputation is too low. – ignacy130 Oct 01 '13 at 11:17
  • Just wanted to know weather it worked or not. it's ok with the reputation issue :-) – Anobik Oct 01 '13 at 11:24
  • ` NavigationCacheMode ` yes that is necessary to force save pages in cache :) – Anobik Oct 01 '13 at 11:26
1

Ok, I solved my problem. Important is to have NavigationCacheMode enabled. In OnNavigatedTo method I just check whether list which to I saved data from database contains any elements. At first page opening it is empty so I load data from database to my list. Thus enabling NavigationCacheMode, data in variables are stored in cache, and loaded while navigating to this page. Some flags may be required.

ignacy130
  • 322
  • 1
  • 2
  • 17