是否可以有不同的IsolatedStorageSettings.ApplicationSettings?
我遇到的问题是,我对 ApplicationSettings 所做的更改似乎没有在我的 AudioPlayerAgents ApplicationSettings 中更新,这应该是相同的?!
我的程序如下所示:
在 OnNavigedTo 的 MainPage.xaml.cs 中,我创建两个音频文件数组
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));
然后我将其中一个数组保存在 ApplicationSettings 中
IsolatedStorageSettings.ApplicationSettings["tracklist"] = aud;
IsolatedStorageSettings.ApplicationSettings.Save();
然后我关闭并启动 BackgroundAudioPlayer。
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Play();
在我的 AudioPlayer 中,我正在加载之前保存的 ApplicationSettings,它工作得很好。
Audio[] aud;
IsolatedStorageSettings.ApplicationSettings.TryGetValue<Audio[]>("tracklist", out aud);
但是当我稍后想要用另一个数组替换 MainPage.xaml.cs 中的 ApplicationSettings
IsolatedStorageSettings.ApplicationSettings["tracklist"] = aud1;
IsolatedStorageSettings.ApplicationSettings.Save();
并在我的 AudioPlayer 中再次加载值时,我的 ApplicationSettings 中仍然有旧值,AudioPlayerAgent 和 MainPage 都应该使用相同的 ApplicationSettings ?事实上,第一次保存并可供 AudioPlayerAgent 使用时,我错过了什么?
我的音频课看起来像这样
[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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一种感觉,你的 MusicPlayerAgent 位于另一个程序集/dll 中。如果这样做就可以解释问题,因为每个程序集都有自己的独立存储。如果它们在同一个程序集中,我不知道为什么这行不通,因为我自己在几乎所有手机应用程序中都这样做了。这是我读过的有关隔离存储的最佳读物。如果有的话,我希望该链接值得一读。 链接
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
我遇到了同样的问题。在我看来,IsolatedStorageSettings 有点“缓存”到静态的东西中。也就是说,在后台和前台进程都运行之前,每个进程都将使用自己版本的isolatedStorageSettings。
深入研究原始代码,我发现以下内容:
在这里您可以看到,IsolatedStorageSettings 实际上在方法 Reload 中每个进程仅加载一次(因为它是静态变量)。
翻了一下代码,没有发现其他地方调用了Reload。
我可以建议每个面临同样问题的人使用“自己的”设置存储来在 AudioAgent 和应用程序之间共享动态数据(正如 Philiiiiiipp 在他的最后 评论)
据我所知,最佳实践是使用 AudioTrack.Tag 属性。
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:
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.
我不明白isolatedStorage是如何在电话上工作的。 PC 应用程序中的相同命名空间具有显式 Scope 属性,因此您至少可以判断是否存在单独的父文件夹。
如果您无法通过简单地将两个项目中的所有文件加载到另一个项目中来将两个项目合并为一个,那么至少您可以向其中一个项目添加一个类或方法,该项目从isolatedStorage加载该类并返回一个实例,然后调用它来自另一个项目,将引用(在解决方案资源管理器的引用文件夹中)添加到第二个项目中的第一个项目,以便您可以调用它。
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.