繁体   English   中英

如何在WP7中保存/加载自定义类类型的ObservableCollection

[英]How to save/load an ObservableCollection of custom class type in WP7

我创建了一个名为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中的项目,但一旦关闭后应用程序被激活,则不能使用它们吗?

我在之前的项目中有同样的要求

创建一个类来保存和读取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);
            }
        }

    }

在ISO Store中保存数据

public ObservableCollection<CustomClass> AllEvents = new public ObservableCollection<CustomClass>();

//AllEvents.Add(customclassref1);
//AllEvents.Add(customclassref2);
//AllEvents.Add(customclassref3);
SerializeHelper.SaveData<ObservableCollection<CustomClass>>("AllEvents", AllEvents);

从ISO商店检索数据

AllEvents = (ObservableCollection<CustomClass>)SerializeHelper.ReadData<ObservableCollection<CustomClass>>("AllEvents");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM