1

我创建了一个自定义类,命名为BrowserItem我在我的应用程序中使用,其中包含很多值。我在 ObservableCollection 中使用此类来存储项目集合。出于某种原因,尽管此实现在我的应用程序运行时有效,但当应用程序关闭然后重新打开时,我无法正确地使这些值保持不变。我不确定如何正确保存然后重新加载 BrowserItem 类型的 ObservableCollection。

浏览器项.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(我的类从隔离存储中保存和加载)

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 中的项目可以在应用程序处于活动状态时使用,但不能在应用程序关闭后激活后使用?

4

1 回答 1

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);
            }
        }

    }

在 ISO 存储中保存数据

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");
于 2012-09-25T06:22:52.100 回答