1

我正在涉足 Windows Phone 开发,并开始接受 WP 上 Silverlight 的一些功能,但我正在努力使用 XML:

我正在尝试将一些对象序列化为 XML,然后读取所述 XML 并将其再次序列化为对象。然后我将使用它来填充列表框,方法是把一个ObservableCollection作为ItemsSource列表框的。

我已经确保数据绑定正常工作;如果我只是生成对象并将它们放入 anObservable Collection然后将其作为ItemsSource,则没有问题。似乎是我的代码的 XML 部分出错了。一切都编译并执行得很好,但列表框仍然是空的:(

此代码在应用程序启动时执行(不是很有效,但适用于我的测试):

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Quote> quotes = new ObservableCollection<Quote>();
        for (int i = 0; i < 10; i++)
        {
            Quote quote = new Quote()
            {
                Author = "Author #" + i.ToString(),
                QuoteText = "This is quote #" + i.ToString(),
            };

            quotes.Add(quote);
        }

        XmlWriterSettings xmlwrtrstngs = new XmlWriterSettings();
        xmlwrtrstngs.Indent = true;
        using(IsolatedStorageFile isostrg = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using(IsolatedStorageFileStream isoflstrm = isostrg.OpenFile("Quotes.xml", FileMode.Create))
            {
                XmlSerializer xmlsrlzr = new XmlSerializer(typeof(QuoteCollection));
                using(XmlWriter xmlwrtr = XmlWriter.Create(isoflstrm, xmlwrtrstngs))
                {
                    xmlsrlzr.Serialize(xmlwrtr, quoteCollection);
                }
            }
        }

        loadData();
    }

    void loadData()
    {
        try
        {
            using(IsolatedStorageFile isostrg = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using(IsolatedStorageFileStream isoflstrm = isostrg.OpenFile("Quotes.xml", FileMode.Open))
                {
                    XmlSerializer xmlsrlzr = new XmlSerializer(typeof(QuoteCollection));
                    QuoteCollection quoteCollectionFromXML = (QuoteCollection)xmlsrlzr.Deserialize(isoflstrm);
                    LstBx.ItemsSource = quoteCollectionFromXML.Quotes;
                }
            }
        }
        catch(Exception)
        {

            Console.Write("Something went wrong with the XML!");
        }


    }

QuoteCollection

public class QuoteCollection : INotifyPropertyChanged
{
    ObservableCollection<Quote> quotes;
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Quote> Quotes
    {
        get { return quotes; }

        set
        {
            if(quotes != value)
            {
                quotes = value;
                raisePropertyChanged("Quotes");
            }
        }
    }

    protected virtual void raisePropertyChanged(string argPropertyChanged)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(argPropertyChanged));
        }
    }
}

引用

public class Quote : INotifyPropertyChanged
{
    string author;
    string quoteText;
    public event PropertyChangedEventHandler PropertyChanged;

    public string Author
    {
        get
        {
            return author;
        }

        set
        {
            if(author != value)
            {
                author = value;
                onPropertyChanged("Author");
            }
        }
    }

    public string QuoteText
    {
        get
        {
            return quoteText;
        }

        set
        {
            if(quoteText != value)
            {
                quoteText = value;
                onPropertyChanged("QuoteText");
            }
        }
    }

    protected virtual void onPropertyChanged(string argProperty)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(argProperty));
        }
    }
}

任何见解将不胜感激:)

4

2 回答 2

2

您正在序列化 usingQuoteCollection作为类型,但实际上写出一个ObservableCollection<Quote>.

于 2012-06-03T14:56:21.170 回答
0

忘记将 ObservableCollection 放入我之前创建的 QuoteCollection 实例中(未在发布的代码中显示)。

于 2012-06-01T14:41:57.503 回答