简体   繁体   中英

Windows Phone Development - IsolatedStorage

I am using IsolatedStorage to communicate with the Audio agent as below:

In each of my pages:

 private void playButton_Click(object sender, RoutedEventArgs e)
    {

        if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
        {
            BackgroundAudioPlayer.Instance.Pause();
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings["BtnClicked"] = "1"; (or 2 or 3)

            IsolatedStorageSettings.ApplicationSettings.Save();
            BackgroundAudioPlayer.Instance.Stop();
            BackgroundAudioPlayer.Instance.Play();

        }
    }

In my AudioPlayer.cs:

`case UserAction.Play:

    if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "1")
    {
        _playList = _playList1;
    }

    else if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "2")
    {

        _playList = _playList;
    }

    else if ((string)IsolatedStorageSettings.ApplicationSettings["BtnClicked"] == "3")
    {
        _playList = _playList2;            
    }
        PlayTrack(player);        `

The problem however is that the "_playlist" variable isnt being updated except the first time. For example, if I Open Page 1, it selects _playlist1 correctly, but if I press "Back" then enter Page 2, it still selects _Playlist1. How can I force the variable to update every time I select a new page in my app? Also the rest of the code is very similar to: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978%28v=vs.105%29.aspx

MSDN has some guidelines for best practices with background agents:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202944(v=vs.105).aspx#BKMK_CommunicationBetweenForegroundApplicationandBackgroundAgent

Notably MSDN suggests NOT to use IsolatedStorageSettings to communicate between the foreground app and background agent. You should instead use a SQL table, or a file in isolated storage protected by a mutex.

It's not updated because IsolatedStorageSettings.ApplicationSettings value is cached in a static variable, and there's no way to force it to reload from the isolated storage.

Instead you should read/write an isolated storage file, guarded by the named mutex.

BTW, it's a good idea to place that code, along with the file+mutex names, in an assembly shared between GUI and background processes: this way you'll be sure both your processes will use same data and same mutex.

PS Unfortunately, named mutexes are the only interprocess synchronization methods available on the platform: no semaphores, no events, no windows messages, no silverlight's local messages, nothing..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM