[英]Saving data in Windows Phone application for user settings in Windows Phone using a global class
I'm a iOS developer starting to learn Windows Phone development. 我是一名iOS开发人员,开始学习Windows Phone开发。 I have a question about saving data in a Windows Phone app like you do with
NSUserDefaults
in iOS
. 我有一个关于在Windows Phone应用程序中保存数据的问题,
NSUserDefaults
在iOS
使用NSUserDefaults
一样。
I'm developing an app using the Windows Runtime API and not the Silverlight API. 我正在使用Windows Runtime API而不是 Silverlight API开发应用程序。 I've seen that Silverlight as something called
Isolated Storage
and Windows runtime API has Application Data Storage
. 我已经看到Silverlight作为一种称为“
Isolated Storage
和Windows运行时API的东西具有“ Application Data Storage
。
My question is if somebody with more experience in Windows Phone development and C#
could show me and guide me on how to create a global class that I can use to store settings in my Windows Phone app. 我的问题是,是否有在Windows Phone开发和
C#
方面有更多经验的人可以向我展示并指导我如何创建可用于在Windows Phone应用程序中存储设置的全局类。
In iOS I have a class that uses NSUserDefaults to store and retrive for example if the user is logged in or not etc. It looks like this: 在iOS中,我有一个使用NSUserDefaults进行存储和检索的类,例如,如果用户未登录等。它看起来像这样:
//
// RPStoreNSUserDefault.m
//
+ (RPStoreNSUserDefault *)sharedInstance
{
static RPStoreNSUserDefault *shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[RPStoreNSUserDefault alloc] init];
});
return shared;
}
- (void)storeObject:(id)object forKey:(NSString*)key
{
NSData *serializer = [NSKeyedArchiver archivedDataWithRootObject:object];
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setObject:serializer forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (id)retrieveObjectForKey:(NSString*)key
{
NSData *serializer = [[NSUserDefaults standardUserDefaults] objectForKey:key];
return [NSKeyedUnarchiver unarchiveObjectWithData:serializer];
}
How would I do the same thing using what I understad the Application Data Storage
in Windows Phone and c#? 我如何利用Windows Phone和c#中的
Application Data Storage
来做同样的事情?
Thanks in advance. 提前致谢。
Here's a Storage service I've created for working with WinRT both on Store and Phone. 这是我为在商店和电话上使用WinRT而创建的一项存储服务。
It uses James Newtons Json.net to serialize and deserialize objects to json before storing them and makes full use of generics. 它使用James Newtons Json.net将对象序列化和反序列化为json,然后再存储它们,并充分利用泛型。
You might want to look into StorageFile as well. 您可能还想研究StorageFile。 See Jerry Nixons blogpost: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
参见杰里·尼克松(Jerry Nixons)博客文章: http : //blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
public class StorageService : IStorageService
{
#region Settings
public void SaveSetting(string key, string value)
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values[key] = value;
}
public void DeleteSetting(string key)
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values.Remove(key);
}
public string LoadSetting(string key)
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
var value = localSettings.Values[key];
if (value == null)
{
return null;
}
return value.ToString();
}
#endregion
#region Objects
public async Task<bool> PersistObjectAsync<T>(string key, T value)
{
if (string.IsNullOrEmpty(key) || value == null)
{
throw new ArgumentNullException();
}
Windows.Storage.StorageFolder localFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
var file = await localFolder.CreateFileAsync(key, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, json);
return true;
}
public async Task<T> RetrieveObjectAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException();
}
var localFolder = ApplicationData.Current.LocalFolder;
try
{
var file = await localFolder.GetFileAsync(key);
string json = await FileIO.ReadTextAsync(file);
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception exp)
{
Debug.WriteLine(exp.Message);
return default(T);
}
}
public async Task<bool> DeleteObjectAsync(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException();
}
Windows.Storage.StorageFolder localFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
try
{
StorageFile file = await localFolder.GetFileAsync(key);
await file.DeleteAsync();
return true;
}
catch (Exception exp)
{
Debug.WriteLine(exp.Message);
return false;
}
}
#endregion
}
public interface IStorageService
{
void SaveSetting(string key, string value);
void DeleteSetting(string key);
string LoadSetting(string key);
Task<bool> PersistObjectAsync<T>(string key, T value);
Task<T> RetrieveObjectAsync<T>(string key);
Task<bool> DeleteObjectAsync(string key);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.