当BackgroundAudioPlayer的实例处于活动状态时,如何在IsolatedStorage中保存设置?

我的解决方案中有两个项目。 让我们说项目A和项目B.

项目A.

这是主要项目,并有设置。 我有一个复选框,为用户提供“重复”曲目的选项。 该项目还可以访问PROJECT B的公共实例。

项目B.

它是BackgroundAudioAgent并且拥有自己的设置。 此项目无权访问PROJECT A设置。 因此,在PROJECT A中,我需要访问PROJECT B的设置并将其保存在那里。 因此,当启用“重复”时,代理会重新开始播放。

问题

当BackgroundAudioPlayer的实例运行时,我无法保存设置(换句话说,设置已保存,但不会产生任何影响)。 我总是要关闭实例,当我这样做时,可以更改设置。

  1. 做我想做的事最有效的方法是什么?

  2. 如何在不关闭BackgroundAudioPlayer实例的情况下保存IsolatedStorage中的设置? (因为我不想打断正在播放的任何曲目)。

代码:我需要做些什么来保存设置。

public bool SettingAudioRepeat { get { return GetValueOrDefault(SettingAudioRepeatKeyName, SettingAudioRepeatDefault); } set { if (AddOrUpdateValue(SettingAudioRepeatKeyName, value)) { bool resumePlay = false; try { if (BackgroundAudioPlayer.Instance.PlayerState != PlayState.Shutdown) { BackgroundAudioPlayer.Instance.Close(); resumePlay = true; } } catch { } TaskEx.Delay(300); IQR_Settings iqrSet = new IQR_Settings(); iqrSet.SettingAudioRepeat = value; iqrSet.Save(); //Saving the settings for Project B Save(); //Saving the settings for Project A try { if (resumePlay) BackgroundAudioPlayer.Instance.Play(); //It starts all from scracth } catch { } } } public T GetValueOrDefault(string Key, T defaultValue) { T value; // If the key exists, retrieve the value. if (settings.Contains(Key)) { value = (T)settings[Key]; } // Otherwise, use the default value. else { value = defaultValue; } return value; } 

代码:我只想做什么。

  public bool SettingAudioRepeat { get { return GetValueOrDefault(SettingAudioRepeatKeyName, SettingAudioRepeatDefault); } set { if (AddOrUpdateValue(SettingAudioRepeatKeyName, value)) { IQR_Settings iqrSet = new IQR_Settings(); iqrSet.SettingAudioRepeat = value; iqrSet.Save(); //Saving the settings for Project B Save(); //Saving the settings for Project A } } 

我同意背景音频是一个乳房。 每当使用任何后台代理时,您都不能依赖ApplicationSettings进行同步。 如果您想从UI(应用程序)和后台(音频代理)保存和访问设置,则应保存文件。 您可以使用Json.Net序列化设置并将文件保存到已知位置。 以下是可能的样本

 // From background agent var settings = Settings.Load(); if(settings.Foo) { // do something } 

这是一个示例设置文件。 需要定期保存设置。

 public class Settings { private const string FileName = "shared/settings.json"; private Settings() { } public bool Foo { get; set; } public int Bar { get; set; } public static Settings Load() { var storage = IsolatedStorageFile.GetUserStoreForApplication(); if (storage.FileExists(FileName) == false) return new Settings(); using (var stream = storage.OpenFile(FileName, FileMode.Open, FileAccess.Read)) { using (var reader = new StreamReader(stream)) { string json = reader.ReadToEnd(); if (string.IsNullOrEmpty(json) == false) { return JsonConvert.DeserializeObject(json); } } } return new Settings(); } public void Save() { var storage = IsolatedStorageFile.GetUserStoreForApplication(); if(storage.FileExists(FileName)) storage.DeleteFile(FileName); using (var fileStream = storage.CreateFile(FileName)) { //Write the data using (var isoFileWriter = new StreamWriter(fileStream)) { var json = JsonConvert.SerializeObject(this); isoFileWriter.WriteLine(json); } } } } 

我个人有一个FileStorage类,用于保存/加载数据。 我到处都用它。 它在这里(它确实使用Mutex来阻止从后台代理和应用程序访问该文件)。 您可以在此处找到FileStorage类 。