Reputation: 566
My application has two pages, which we will call TheMenu and TheForm. When the user leaves TheForm and returns to TheMenu, TheForm.OnNavigatedFrom()
will save state to isolated storage, and TheMenu.OnNavigatedTo()
will read some of that state from the isolated storage to re-initialize TheMenu.
The problem is that since storage access is asynchronous, TheMenu.NavigatedTo()
will begin reading state before TheForm.OnNavigatedFrom()
has finished writing it.
I'm thinking of handling this by creating application-wide instance of AsyncLock
(implementation provided here: http://www.hanselman.com/blog/ComparingTwoTechniquesInNETAsynchronousCoordinationPrimitives.aspx). I would acquire the lock at the top of OnNavigatedTo()
, and release it from a finally {}
block at the bottom of OnNavigatedFrom()
.
Does anyone foresee any issues with this approach? Is there a better alternative?
Upvotes: 0
Views: 82
Reputation: 12019
Although it is good practice to write state out to disk at appropriate intervals, for something like this you should use an in-memory representation rather than round-tripping via the disk. There are many ways to design it, but one common approach is to have a static property off your App
class that holds the shared state. You can save it to disk when navigating away from pages (as you already do) but when you navigate to a page, first check if there is a valid object stored in the App
's property before trying to read from disk.
Upvotes: 1