我怎样才能保存设置在IsolatedStorage而BackgroundAudioPlayer的情况下被激活? [英] How can I save the settings in the IsolatedStorage while BackgroundAudioPlayer's instance is active?

查看:195
本文介绍了我怎样才能保存设置在IsolatedStorage而BackgroundAudioPlayer的情况下被激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的解决方案两个项目。比方说,项目A和项目B.

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

项目A

这是主项目,他有设置。我有一个复选框,给予用户一个选项,重复音轨。该项目还可以访问项目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并拥有它自己的设置。本项目不具备访问项目中的设置。因此,在项目中,我需要访问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?

我如何保存在IsolatedStorage中的设置,但不关闭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).

代码:我要做的保存设置。

    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;
    }



代码:我只是想做的事情。

CODE: What I simply want to do.

    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(APP)和背景(音频代理)访问设置,您应该保存文件。您可以使用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.

这篇关于我怎样才能保存设置在IsolatedStorage而BackgroundAudioPlayer的情况下被激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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