当BackgroundAudioPlayer 的实例处于活动状态时,如何将设置保存在IsolatedStorage 中? [英] How can I save the settings in the IsolatedStorage while BackgroundAudioPlayer's instance is active?

查看:18
本文介绍了当BackgroundAudioPlayer 的实例处于活动状态时,如何将设置保存在IsolatedStorage 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有两个项目.假设项目 A 和项目 B.

I have Two projects in my Solution. Let's say Project A and Project B.

A 项目

它是主项目并有设置.我有一个复选框,可以让用户选择重复"曲目.该项目还可以访问PROJECT B的公共实例.

It's the main project and has settings. I have a check box to give user an option to "Repeat" track(s). This project can also access PROJECT B's public instances.

项目 B

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

It's the BackgroundAudioAgent and has it's own settings. This project doesn't have access to PROJECT A settings. Therefore, in PROJECT A , I need to access the settings of PROJECT B and save it there. So that, when the "Repeat" is enabled, the agent restarts playing.

问题

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

I am unable to save the settings (in other words, the settings are saved, but it does not take any affect) when the BackgroundAudioPlayer's instance is running. I always have to close the instance, and when I do that, the settings can be changed.

问题

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

  1. What is the most efficient way to do what I am trying to do?

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

How can I save the settings in the IsolatedStorage without closing the BackgroundAudioPlayer's instance? (as I don't want to interrupt any track being played).

代码:我必须做什么才能保存设置.

CODE: What I have to do to save settings.

    public bool SettingAudioRepeat
    {
        get
        {
            return GetValueOrDefault<bool>(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<T>(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<bool>(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 序列化设置并将文件保存到已知位置.这是可能看起来像的示例

I agree that Background Audio is a breast. Whenever using any background agent you cannot rely on the ApplicationSettings to be synced. If you want to have settings saved and accessed from the UI (app) and background (audio agent) you should save a file. You can serialize the settings using Json.Net and save a file to a known location. Here is sample of what is might look like

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

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

And here is a sample Settings File. The settings would need to be saved on a regular basis.

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<Settings>(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 类,用于保存/加载数据.我到处都用它.在这里(并且它确实使用互斥锁来防止从后台代理和应用程序访问文件).您可以在 此处找到 FileStorage 类.

I personally have a FileStorage class that I use for saving/loading data. I use it everywhere. Here it is (and it does use the Mutex to prevent access to the file from both background agent and app). You can find the FileStorage class here.

这篇关于当BackgroundAudioPlayer 的实例处于活动状态时,如何将设置保存在IsolatedStorage 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆