gpt4 book ai didi

c# - 在 IsolatedStorageSettings 中存储对象

转载 作者:太空狗 更新时间:2023-10-30 00:44:37 27 4
gpt4 key购买 nike

我有一个对象要存储在 IsolatedStorageSettings 中,我不想在应用程序重新启动时重用它。

我的问题在于我出于某种原因编写的代码在重新启动时尝试访问 key 时不记得该对象。

namespace MyNameSpace
{
public class WindowsPhoneSettings
{
private const string SelectedSiteKey = "SelectedSite";
private IsolatedStorageSettings isolatedStore = IsolatedStorageSettings.ApplicationSettings;

private T RetrieveSetting<T>(string settingKey)
{
object settingValue;
if (isolatedStore.TryGetValue(settingKey, out settingValue))
{
return (T)settingValue;
}
return default(T);
}

public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;

if (isolatedStore.Contains(Key))
{
if (isolatedStore[Key] != value)
{
isolatedStore[Key] = value;
valueChanged = true;
}
}
else
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}

public MobileSiteDataModel SelectedSite
{
get
{
return RetrieveSetting<MobileSiteDataModel>(SelectedSiteKey);
}
set
{
AddOrUpdateValue(SelectedSiteKey, value);
isolatedStore.Save();
}
}
}
}

然后我在 App.xaml.cs 中实例化 WindowsPhoneSettings 并为其创建一个公共(public) getter 和 setter。为了能够在整个应用程序中访问它。调试显示正确的对象存储在隔离存储中,但是当关闭应用程序并重新打开它时,隔离存储似乎是空的。我在模拟器和真实设备上都试过了。如您所见,我在设置对象时确实调用了保存方法。

我在这里做错了什么?

最佳答案

我最终将设置保存到隔离存储中的一个文件中,因为 IsolatedStorageSettings 似乎从未起作用。

所以我的代码最终是这样的:

public class PhoneSettings
{
private const string SettingsDir = "settingsDir";
private const string SettingsFile = "settings.xml";

public void SetSettings(Settings settings)
{
SaveSettingToFile<Settings>(SettingsDir, SettingsFile, settings);
}

public Settings GetSettings()
{
return RetrieveSettingFromFile<Settings>(SettingsDir, SettingsFile);
}

private T RetrieveSettingFromFile<T>(string dir, string file) where T : class
{
IsolatedStorageFile isolatedFileStore = IsolatedStorageFile.GetUserStoreForApplication();
if (isolatedFileStore.DirectoryExists(dir))
{
try
{
using (var stream = new IsolatedStorageFileStream(System.IO.Path.Combine(dir, file), FileMode.Open, isolatedFileStore))
{
return (T)SerializationHelper.DeserializeData<T>(stream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Could not retrieve file " + dir + "\\" + file + ". With Exception: " + ex.Message);
}
}
return null;
}

private void SaveSettingToFile<T>(string dir, string file, T data)
{
IsolatedStorageFile isolatedFileStore = IsolatedStorageFile.GetUserStoreForApplication();
if (!isolatedFileStore.DirectoryExists(dir))
isolatedFileStore.CreateDirectory(dir);
try
{
string fn = System.IO.Path.Combine(dir, file);
if (isolatedFileStore.FileExists(fn)) isolatedFileStore.DeleteFile(fn); //mostly harmless, used because isolatedFileStore is stupid :D

using (var stream = new IsolatedStorageFileStream(fn, FileMode.CreateNew, FileAccess.ReadWrite, isolatedFileStore))
{
SerializationHelper.SerializeData<T>(data, stream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Could not save file " + dir + "\\" + file + ". With Exception: " + ex.Message);
}
}
}

还有一个设置类,只包含我要保存的内容。这可能是:

class Settings
{
private string name;
private int id;

public string Name
{
get { return name; }
set { name = value; }
}

public int Id
{
get { return id; }
set { id = value; }
}
}

编辑:如何实现 SerializationHelper 的示例

public static class SerializationHelper
{
public static void SerializeData<T>(this T obj, Stream streamObject)
{
if (obj == null || streamObject == null)
return;

var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(streamObject, obj);
}

public static T DeserializeData<T>(Stream streamObject)
{
if (streamObject == null)
return default(T);

var ser = new DataContractJsonSerializer(typeof(T));
return (T)ser.ReadObject(streamObject);
}
}

关于c# - 在 IsolatedStorageSettings 中存储对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415864/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com