Is it possible to have different IsolatedStorageSettings.ApplicationSettings?

785 Views Asked by At

I have the problem that it seems like the changes I do to my ApplicationSettings are not updated in my AudioPlayerAgents ApplicationSettings which should be the same ?!

My program looks like this:

In my MainPage.xaml.cs in the OnNavigatedTo I am creating two arrays of Audio Files

Audio[] aud = new Audio[2];
Audio[] aud1 = new Audio[2];

aud[0] = new Audio(new Uri("1.mp3", UriKind.Relative), 
                   "Test1", 
                   "Test1",
                   new Uri("Images/Covers/0000000018724345_256x256_large.jpg",                       UriKind.Relative));

aud[1] = new Audio(new Uri("2.mp3", UriKind.Relative), 
                   "Test2", 
                   "Test2",
                   new Uri("Images/Covers/0000000018698018_256x256_large.jpg", UriKind.Relative));

aud1[0] = new Audio(new Uri("3.mp3", UriKind.Relative), 
                   "Test3", 
                   "Test3",
                   new Uri("Images/Covers/0000000018465020_256x256_large.jpg", UriKind.Relative));

 aud1[1] = new Audio(new Uri("http://traffic.libsyn.com/wpradio/WPRadio_29.mp3", UriKind.Absolute),
                   "Episode 29",
                   "Windows Phone Radio",
                   new Uri("Images/Covers/0000000018844939_256x256_large.jpg", UriKind.Relative));

Then I am saving one of this arrays in the ApplicationSettings

IsolatedStorageSettings.ApplicationSettings["tracklist"] = aud;
IsolatedStorageSettings.ApplicationSettings.Save();

Then I am closing and starting the BackgroundAudioPlayer.

BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Play();

In my AudioPlayer I am loading the previously saved ApplicationSettings which works fine.

Audio[] aud;
IsolatedStorageSettings.ApplicationSettings.TryGetValue<Audio[]>("tracklist", out aud);

But when I later want to replace the ApplicationSettings in my MainPage.xaml.cs with the other array

  IsolatedStorageSettings.ApplicationSettings["tracklist"] = aud1;
  IsolatedStorageSettings.ApplicationSettings.Save();

And load the values again in my AudioPlayer there are still the old values in my ApplicationSettings, both the AudioPlayerAgent and the MainPage should use the same ApplicationSettings right ? In fact the first time it is saved and available to the AudioPlayerAgent, so what am I missing ?

My Audio class looks like this

[DataContractAttribute] 
public class Audio
{
    [DataMember]
    public Uri TrackUrl { get; set; }

    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public string Artist { get; set; }

    [DataMember]
    public Uri CoverURL { get; set; }

    public Audio(Uri trackUrl, string title, string artist, Uri coverUrl)
    {
        TrackUrl = trackUrl;
        Title = title;
        Artist = artist;
        CoverURL = coverUrl;
    }
} 
3

There are 3 best solutions below

3
On BEST ANSWER

I have a feeling that you have the MusicPlayerAgent in another assembly/dll. If you do that would explain the problem since each assembly has its own isolated storage. If they are in the same assembly I dont have any idea why that wouldnt work since I do that myself in almost all my phone apps I have. Here is the best read on Isolated Storage I have read. If anything I hope the link is a good read. Link

0
On

I faced the same problem. It seems to me that the IsolatedStorageSettings are kind-of "cached" into something static. That is, until the both background and foreground processes are running, each of them will use own version of IsolatedStorageSettings. Getting deeper into the original code I found following:

public sealed class IsolatedStorageSettings : ...
{
    private static IsolatedStorageSettings s_appSettings;
    ...
    public static IsolatedStorageSettings ApplicationSettings
    {
        get
        {
            if (IsolatedStorageSettings.s_appSettings == null)
            {
                IsolatedStorageSettings.s_appSettings = new IsolatedStorageSettings(false);
            }
            return IsolatedStorageSettings.s_appSettings;
        }
    }
    ...
    private IsolatedStorageSettings(bool useSiteSettings)
    {
        if (useSiteSettings)
        {
            throw new NotSupportedException();
        }
        this._appStore = IsolatedStorageFile.GetUserStoreForApplication();
        this.Reload();
    }

Here you can see that IsolatedStorageSettings are actually loaded only once per process (as it's static variable) in method Reload. Looking through the code, I found no other places where Reload is called.

I can suggest everyone facing the same issue to use "own" settings storage to share dynamic data between AudioAgent and App (as Philiiiiiipp said in his final comment )

From what I know, best practice is to use AudioTrack.Tag property.

0
On

I don't understand how IsolatedStorage has been hobbled to work on Phone. Same namespace in PC apps has an explicit Scope property so you could at least tell whether there are separate parent folders.

If you can't combine the 2 projects into one by simply loading all the files from one into the other, at least you could add a class or method to one of the projects which loads the class from IsolatedStorage and returns an instance, then call it from the other project, adding a reference (in the References folder in Solution Explorer) to the first project in the second so you can invoke it.