我创建了一个名为BrowserItem
的自定义类,我在我的应用程序中使用它,它包含很多值。我在ObservableCollection中使用此类来存储项目集合。出于某种原因,虽然这个实现在我的应用程序运行时有效,但是当应用程序关闭然后重新打开时,我无法正确地保持这些值。我不确定如何正确保存然后重新加载BrowserItem类型的ObservableCollection。
BrowserItem.cs
[DataContract]
public class BrowserItem
{
[DataMember]
public FullWebBrowser Browser
{
get;
set;
}
[DataMember]
public string Url
{
get;
set;
}
[DataMember]
public BitmapImage ImageUri
{
get;
set;
}
[DataMember]
public string Title
{
get;
set;
}
[DataMember]
public string Notification
{
get;
set;
}
[DataMember]
public bool DisplayNotification
{
get
{
return !string.IsNullOrEmpty(this.Notification);
}
}
[DataMember]
public string Message
{
get;
set;
}
[DataMember]
public string GroupTag
{
get;
set;
}
[DataMember]
//for translation purposes (bound to HubTile Title on MainPage)
public string TileName
{
get;
set;
}
}
Setting.cs(我的类从IsolatedStorage保存和加载)
public class Setting<T>
{
string name;
T value;
T defaultValue;
bool hasValue;
public Setting(string name, T defaultValue)
{
this.name = name;
this.defaultValue = defaultValue;
}
public T Value
{
get
{
//Check for the cached value
if (!this.hasValue)
{
//Try to get the value from Isolated Storage
if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))
{
//It hasn't been set yet
this.value = this.defaultValue;
IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
}
this.hasValue = true;
}
return this.value;
}
set
{
//Save the value to Isolated Storage
IsolatedStorageSettings.ApplicationSettings[this.name] = value;
this.value = value;
this.hasValue = true;
}
}
public T DefaultValue
{
get { return this.defaultValue; }
}
// Clear cached value
public void ForceRefresh()
{
this.hasValue = false;
}
}
Settings.cs(初始化ObservableCollection的地方)
public static Setting<ObservableCollection<BrowserItem>> BrowserList = new Setting<ObservableCollection<BrowserItem>>("Browsers", new ObservableCollection<BrowserItem>());
public static Setting<string> InitialUri = new Setting<string>("InitialUri", "http://www.bing.com");
在上面的类中,InitialUri在保存新值并稍后使用时工作正常,但我认为ObservableCollection的问题是它的类型为BrowserItem
。我不知道怎么做,所以BrowserItem
将能够在ObservableCollection中用来保存和检索添加到ObservableCollection的项目。添加项目的示例如下
TabsPage.xaml.cs
void addNew_Click(object sender, EventArgs e)
{
BitmapImage newTileImage = new BitmapImage();
var newItem = new BrowserItem() { Browser = new FullWebBrowser(), Url = "http://www.bing.com", ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new browser", GroupTag = "TileGroup", TileName = "new" };
newItem.Browser.InitialUri = Settings.InitialUri.Value;
Settings.BrowserList.Value.Add(newItem);
}
ObservableCollection中的项目可以在应用程序处于活动状态时使用,但一旦关闭后应用程序被激活就不能使用了吗?
答案 0 :(得分:3)
我在之前的项目中有同样的要求
创建一个类来保存和读取ObservableCollection
中的数据public class SerializeHelper
{
public static void SaveData<T>(string fileName, T dataToSave)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
if (store.FileExists(fileName))
{
store.DeleteFile(fileName);
}
using (IsolatedStorageFileStream stream = store.OpenFile(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(stream, dataToSave);
}
}
catch (Exception e)
{
//MessageBox.Show(e.Message);
return;
}
}
}
public static T ReadData<T>(string fileName)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(fileName))
{
using (IsolatedStorageFileStream stream = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Read))
{
try
{
var serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
catch (Exception)
{
return default(T);
}
}
}
return default(T);
}
}
}
public ObservableCollection<CustomClass> AllEvents = new public ObservableCollection<CustomClass>();
//AllEvents.Add(customclassref1);
//AllEvents.Add(customclassref2);
//AllEvents.Add(customclassref3);
SerializeHelper.SaveData<ObservableCollection<CustomClass>>("AllEvents", AllEvents);
AllEvents = (ObservableCollection<CustomClass>)SerializeHelper.ReadData<ObservableCollection<CustomClass>>("AllEvents");