Silverlight MVVM Isolated Storage - silverlight

I've tried to use IsolatedStorageSettings in my ViewModel, but these are not getting retained across browser refreshes (F5).
for example;
//look in the IsoStore for remembered details
IsRememberMe = IsolatedStorageSettings.ApplicationSettings.Contains(Constants.LOGIN_REMEMBERED_USERNAME);
if (IsRememberMe)
{
UserName = IsolatedStorageSettings.ApplicationSettings[Constants.LOGIN_REMEMBERED_USERNAME] as string;
}
Do I need to do something differently in my MVVM ViewModel's??
EDIT
It's worth noting that this code is sitting in a referenced project - so ultimately a seperate XAP file to the parent XAP that is loaded in the browser - might this cause the settings to be lost on each refresh?
THanks,
Mark

Well...
In my case I have issues using Application Isolated Storage, each time I deployed a new version of my app (just for instance changing the color of a button I lost my Iso Storage :-().
I move to use SiteStorage instead of Application level, and it worked:
http://www.tipsdotnet.com/TechBlog.aspx?PageIndex=0&BLID=13
On the other hand what I had done with Iso Storage is perform CRUD on folders and files, not sure abou that other kind of settings.
HTH
Braulio

I would think one of two things is happening here. Either your binding isn't working correctly in both directions so either the persistence or the retrieval code is never hit. Or, you're storing these values in application level iso storage from two different applications (or something to that effect). Make sure your code is being hit in both cases (storing and retrieving) and make sure you're accessing the iso store from the same place (if you're using application level isolation, store/retrieve from the same application, etc).

Related

Global Settings & Commands - OO Design Patterns

I am currently building a WPF application and have some system wide settings. Where is the best place to store these settings? App.Config file, Database or some other sort of XML file? (need to read and write).
My other issue is the application has two states (i.e. Admin Mode, Client Mode) and depending on the state the application behaves differently i.e. (Shut Down, Restart, Lock, Unlock etc.) This seems like a good case to implement the State design pattern... but the issue I am facing is that the running application is itself the context class. I am a bit confused... should I be implementing the Singleton Pattern? How is this best implemented?
In general it is often easiest to store application settings using the Settings page of the Project Designer in Visual Studio. These settings can either be stored per user or per application. Either way, they can be saved easily by calling:
Properties.Settings.Default.Save();
They can be accessed similarly:
Properties.Settings.Default.FirstUserSetting = "abc";
You can find out full details by taking a look at the Managing Application Settings page on MSDN.
Regarding your second question, I would recommend using the State Design Pattern. Unfortunately, I didn't understand your problem with that, so please let me know what your issue is and I will try to address it.

Using a Reference Table Service in a WPF Application

For past projects(the last few have been web using asp.net mvc) we created a service that caches our reference tables(as required) to be used primarily for dropdown lists.
Now I'm working on a desktop application.An upgrade from vb6/sybase to vb.net/sql server
I'm trying out WPF.
I started down the same path building up my DAL. one entity for each reference table.
I'm at the stage now where I want to setup the business layer (some reference tables can be edited)
And I'm not sure if I should follow the same process which is to use ReferenceTableService to "manage" the reference tables.(interacts with the DAL, Controller)
This will be an application that sits on a share that multiple users run.
What's the best way to deal with the reference tables? Caching them doesn't seem to be an option. Should I simply load them as each user opens up a new form in the application? Perhaps using a "ReferenceTableService"?
In this case, the Reference Table Service is thin layer in the application. Not a process running elsewhere.
I haven't done much WPF (be interesting to see what the WPF Gurus think) but I think your existing approach is sound and I don;t see why you should deviate from it.
Loading up on app start sounds reasonable; you just have to think about the expected lifetime of a user session vs the expected frequency of changes to the reference data.
Caching: if the data comes from a central service you could always introduce caching there.

Loading a new Silverlight application from isolated storage

Is there any way i can load a silverlight application from isolated storage and replace the current application ?
Short answer: no, you cannot do this. You could however store a dll, read and load it via reflection if you are in a trusted out-of-the-browser application.
Anyway I don't believe this to be a very good practice, it's very likely there is a different better way to solve your particular problem.
You can't replace the currently running application from within Silverlight code.
However you could create a single application to act as a shell. You could store assemblies or entire Xaps in isolated storage. Using the AssemblyPart class allows you to load an assembly from this storage, create an instance of an entry type and execute an entry point method.
The big caveat would be that this "stored application" would have to be coded specifically to work within your shell. You would need to provide your ways in particular to allow for "application" level state to be accessed and a means of loading content assets such as images.

Can two Silverlight applications share IsolatedStorage on one machine?

What identifies an silverlight application and when can two silverlight applications share IsolatedStorage if at all, i.e.:
if I want to have two Silverlight applications share IsolatedStorage space, is this possible? What kind of "application id" do I need to give to do this?
if I don't want two Silverlight applications to share IsolatedStorage, how do I prevent this? Do I need to do this?
For instance, I've noticed when I develop a Silverlight application, I can press F5, in the application save to Isolated Storage, stop the application, press F5 again, and it reads from the same IsolatedStorage. (I would think that a new compilation would cause it to use new IsolatedStorage.)
However, when I then copy the .xap and .html files to another directory and open the .html file, it does NOT share the IsolatedStorage with the application I was developing. What changed?
What is going on behind the scenes here so I know when IsolatedStorage is shared and when it isn't?
The URL to the source XAP file identifies the application. You would want a new version of an application to be able to read the existing store for a previous version. Consider a game where all the high scores are stored in the application store. The user might be a little upset when all those scores disappear after they upgraded it.
Different applications can share a single site based store. However you only get those two levels of granularity, app level keyed at the XAP Url or site based, which is host and port (I'm not sure whether scheme is also part of that key).
If memory serves me right the isolated storage can be used within the scope of the application and scope of the page. So - if I understand my recollection right, I'd probably say - yes.
Edit
From a copy of Pro Silverlight 3.0 in C# that I posess :
(p. 636)
With isloated storage, a niqe storage location is created for every combination of user and application. In other words, the same computer can have multiple isloated storage locations for the same application, assymin each one is for a different user. Similarly the same user can have multiple storage locations one for each Silverlight Application
(p. 637)
... GetUserStoreForFile(). This method provides a storage site that's accessible to all silverlight applications on the same website domain, however these settings are still user specific

Is Silverlight isolated storage treated as permanent, or as a cache?

How persistent is isolated storage - does Silverlight treat it like a cache, deleting items when it needs more space, or only when the user or application request it?
There also seems to be a wide variety of means to identify isolated storage - machine, application, domain, .... What I'm having trouble with is how these all relate to the user. Is it possible, and if so how, to create and later retrieve an isolated storage file with the following properties:
The same file is used, regardless of which Windows user is logged in
The same file is used, regardless of the assembly version (updates to the xap). Instead the url would remain constant. This would have to work even offline (out of browser).
Basically I want the isolated storage to persist across application updates, and over different users logging in.
It is fairly permanent. The user could delete it if they really wanted too, but they would have to go out of their way to do so.
Here is the MSDN documentation for Isolated Storage.
IsolatedStorageFile in Silverlight a couple of statics that let you choose where you want to scope the storage:
GetUserStoreForApplication
GetUserStoreForSite
The "MachineStore" options are not available in Silverlight, there are just "Application" and "Site". Both are scoped by the user..since the files are stored under the user's AppData on Windows. Apps in-browser and out-of-browser share the same Isolated Storage stores.
[edit..I missed part of your question the first time]: The Isolated Storage stores are not part of the browser cache, so they are not cleared when the browser cache is cleared. As a developer, you can delete things programmatically. As a user, you can use the Silverlight configuration UI (i.e. the right-click menu) to manage the stores - it's called "Application Storage" to the user. Finally, an intrepid user can locate the files on disk and delete them manually...they are hidden so they won't show up in a normal search, but a determined user can still find them.

Resources