开发者

Can I get Silverlight Navigation framework to clear its cache?

Basically, I'm using "View-first" style of MVVM hookup - meaning that the view instantiates the viewmodel (well, it grabs it from the service locator which uses a unity container to resolve it but whatever). This works pretty well most of the time - the Navigation framework paradigm seems to work best with the view-first approach, as the querystring is mapped to a view.

Under some conditions I would like a page to recreate itself on navigation, meanin开发者_如何学Pythong that I'd like it to be cleared out of the cache. I haven't yet found a good way to do this.

Something I'm considering is to have the Page listening for a suitable event and set its NavigationCacheMode to "disabled", but I'd rather be able to define this behavior in a central location - like my application controller.


This doesn't sound like it would apply in your situation, but I wanted to post the solution I found for what I needed to do. In my application I needed to clear the entire cache when the user logs out. I want all history to go away, not just a certain page.

My solution was to create a method to clear the cache by changing the size like:

public static void ClearCache()
{
    int size = frame.CacheSize;
    frame.CacheSize = 0;
    frame.CacheSize = size;
}

I then call this after navigating back to the login page:

PageConductor.Navigate("/Login");
PageConductor.ClearCache();

This works if the page has NavigationCacheMode="Enabled". It doesn't work with the Required setting.


Since nobody ever answered I wanted to share my eventual solution. The easiest way I could get this to work is through an attached behavior, which listens for an "clearCache" event. When the event fires, the behavior sets the page cache enabled to false, and that page gets cleared out of the cache.

There are a number of ways to allow the behavior to listen for an event. The easiest is usually to simply pass some sort of an event provider to the behavior in XAML, if that provider can be accessed through the application resources.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜