【发布时间】:2012-05-31 12:42:00
【问题描述】:
我正在涉足 Windows Phone 开发,并开始接受 WP 上 Silverlight 的一些功能,但我在 XML 方面遇到了困难:
我正在尝试将一些对象序列化为 XML,然后读取所述 XML 并将其再次序列化为对象。然后我将通过将ObservableCollection 作为列表框的ItemsSource 来使用它来填充列表框。
我已经确保数据绑定工作正常;如果我只是生成对象并将它们放入Observable 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!");
}
}
引用集合
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));
}
}
}
任何见解将不胜感激:)
【问题讨论】:
-
QuoteCollection是在哪里定义的?您正在使用它作为类型进行序列化,但实际上写出了ObservableCollection<Quote>。 -
提供更多关于 QuoteCollection 类的细节
-
是的,如果此代码编译,则意味着您正在序列化的内容与您使用
Quotes填充的内容完全不同。 -
@tallseth 耶稣,你是对的。不敢相信我直到现在才看到这个。忘记将
ObservableCollection放入我之前创建的QuoteCollection实例中(未在发布的代码中显示)。谢谢一堆伙计:) -
好吧,如果@HiTechMagic 给出了答案,你应该给他一个信任,直到我看到他的评论我才注意到。 :)
标签: c# xml windows-phone-7