使用全局 class 在 Windows Phone 应用程序中保存数据以供 Windows Phone 中的用户设置
Saving data in Windows Phone application for user settings in Windows Phone using a global class
我是一名 iOS 开发人员,开始学习 Windows Phone 开发。我有一个关于在 Windows Phone 应用程序中保存数据的问题,就像在 iOS
.
中使用 NSUserDefaults
一样
我正在使用 Windows 运行时 API 和 而非 Silverlight API 开发应用程序。我已经看到 Silverlight 被称为 Isolated Storage
和 Windows 运行时 API 有 Application Data Storage
.
我的问题是,如果有人在 Windows Phone 开发和 C#
方面有更多经验,可以向我展示并指导我如何创建一个全球 class可以用来在我的 Windows Phone 应用程序中存储设置。
在 iOS 中,我有一个 class,它使用 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];
}
我如何使用我在 Windows Phone 和 c# 中对 Application Data Storage
的理解来做同样的事情?
提前致谢。
这是我为在 Store 和 Phone 上使用 WinRT 而创建的存储服务。
它使用 James Newtons Json.net 将对象序列化和反序列化为 json 并在存储它们之前充分利用泛型。
您可能还想查看 StorageFile。请参阅 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);
}
我是一名 iOS 开发人员,开始学习 Windows Phone 开发。我有一个关于在 Windows Phone 应用程序中保存数据的问题,就像在 iOS
.
NSUserDefaults
一样
我正在使用 Windows 运行时 API 和 而非 Silverlight API 开发应用程序。我已经看到 Silverlight 被称为 Isolated Storage
和 Windows 运行时 API 有 Application Data Storage
.
我的问题是,如果有人在 Windows Phone 开发和 C#
方面有更多经验,可以向我展示并指导我如何创建一个全球 class可以用来在我的 Windows Phone 应用程序中存储设置。
在 iOS 中,我有一个 class,它使用 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];
}
我如何使用我在 Windows Phone 和 c# 中对 Application Data Storage
的理解来做同样的事情?
提前致谢。
这是我为在 Store 和 Phone 上使用 WinRT 而创建的存储服务。
它使用 James Newtons Json.net 将对象序列化和反序列化为 json 并在存储它们之前充分利用泛型。
您可能还想查看 StorageFile。请参阅 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);
}