4

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;
    }
} 
Adam Anderson
  • 339
  • 3
  • 14
Philiiiiiipp
  • 705
  • 1
  • 9
  • 24
  • Have you tried doing a IsolatedStorageSettings.ApplicationSettings.Clear() before saving it again? – Etch Nov 11 '11 at 21:13
  • Yes when I am doing a clear in my MainPage there are still the old values in my MusicPlayerAgent. If I clear the values in my MusicPlayerAgent,than there are no values when I try to read it the next time. – Philiiiiiipp Nov 12 '11 at 11:49
  • Hi Philiiiiiipp, have you managed to solve this issue somehow? I have it too in my WP8 app and even following your discussion on accepted answer did not help (I've made a third project within a solution and a class in it responsible for interactions with isostorage and it did not help). – src091 Mar 24 '13 at 00:17
  • Hey, thats a long time ago, but as far as I remember making another project does not solve it because as stated below it will be packed into another assembly. I did end up writing the settings into a file and reading them out of there (XmlSerializer). – Philiiiiiipp May 08 '13 at 16:37

3 Answers3

1

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.

Community
  • 1
  • 1
Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
1

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

Etch
  • 3,044
  • 25
  • 31
  • Ok, this might be the issue, I have them in different projects and when I go right click on the project -> Properties and change the Assembly Name to be the same in both projects, the app is not starting anymore. How can I put them in the same assembly ? – Philiiiiiipp Nov 13 '11 at 15:33
  • Maybe a different approach is to write a class that is available to the other project that reads and writes from 1 projects isolated storage, this is the route I have went in the past. – Etch Nov 13 '11 at 15:54
  • Yes my current approach is to have a serialiser in between to read the values out of a file, this works fine as well. But I still cannot understand how this IsolatedStorageSettings stuff works. – Philiiiiiipp Nov 13 '11 at 22:16
0

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.

BobHy
  • 1,575
  • 10
  • 23