How are normal people supposed to persist settings in a Windows Phone 8 app?

对着背影说爱祢 提交于 2019-12-18 04:32:06

问题


I'm in the process of writing a Windows Phone 8 app, so I can capture that much sought-after 3% market share, and am having a hard time persisting user settings within the application.

I first ran across this blog which goes over the basics of the Windows.Storage namespace, which is intended to do exactly this sort of thing. Yay!

However, I guess the author never actually ran his own code, as otherwise he would know that the second you call ApplicationData.Current.LocalSettings, you'd get a NotImplementedException exception. To the MSDNs we go!

Well, this makes it pretty clear that this API is not implemented on Windows Phone 8. I came to this conclusion when it said, "This API is not implemented and will throw an exception if called." - Well that's great.

So, maybe there's some other similar APIs. After a bit more Googling, I came across this blog. It's called "Windows 8 Apps - Must Know Tricks!". This looks official! It goes over all sorts of really cool looking persistence APIs, including permanent and transient storage, roaming storage, etc.

But guess what: RoamingFolder, RoamingSettings, TemporaryFolder, LocalSettings - None of it is implemented on Windows Phone 8.

Did implementing these somewhat-key features just slip their mind? Am I supposed to create a local SQL database to store basic app settings, or is there something simple I'm not finding?


回答1:


Ah ha! Figured this out. I dug up the Windows Phone 7 API docs, and the legacy APIs actually still work on Windows Phone 8 as well.

public static void Session_PersistSession(string ticket)
{
   if (IsolatedStorageSettings.ApplicationSettings.Contains("SessionTicket"))
   {
      IsolatedStorageSettings.ApplicationSettings["SessionTicket"] = ticket;
   }
   else
   {
      IsolatedStorageSettings.ApplicationSettings.Add("SessionTicket", ticket);
   }

   IsolatedStorageSettings.ApplicationSettings.Save();
}

public static string Session_LoadSession()
{
   string ticket;
   if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<String>("SessionTicket", out ticket))
   {
      return ticket;
   }

   return null;
}



回答2:


A couple of options here....

  1. LocalFolder is supported so you could serialize whatever state you want to a file there.
  2. Check out the IsolatedStorageSettings class, specifically the ApplicationSettings property

Also this may provide a bit more context: How to preserve and restore app state for Windows Phone



来源:https://stackoverflow.com/questions/13558809/how-are-normal-people-supposed-to-persist-settings-in-a-windows-phone-8-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!