WindowsPhone7中的XML读取、过滤以及数据绑定

我将使用LINQ to XML,以便加载和读取数据,而且我将展示
【菜科解读】
在这个小教程,我将演示在Windows Phone 7如何让ListBox的数据绑定XML数据。
我将使用LINQ to XML,以便加载和读取数据,而且我将展示如何实现一个基本的过滤。
首先让我们先创建一个Windows Phone 7的应用程序项目示例,并添加以下两个demo xml文件。
people.xml
?xml version="1.0" encoding="utf-8" ?> Kate Smith 27
Tom Brown 30
Tim Stone 36
Ann Peterson 27
在这里我不得不感谢一直支持我的卤面网版主,是他让我提起兴趣写了这么一篇文章,再次感谢卤面网,一个非常不错的wp7开发论坛,后面我也将再次向大家发布几篇高质量文章,请大家到卤面上找我吧,呵呵
进入正题:
PeopleCustom.xml
?xml version="1.0" ?>
下一步是创建一个示例类将被用来存储XML元素值:
public class Person{ string firstname; string lastname; int age; public string FirstName { get { return firstname; } set { firstname = value; } } public string LastName { get { return lastname; } set { lastname = value; } } public int Age { get { return age; } set { age = value; } }}
为了读取XML文件的信息,我们将使用的XDocument
所以你首先需要添加System.Xml.Linq.dll引用,然后usingSystem.Xml.Linq;
XDocument loadedData = XDocument.Load("People.xml"); var data = from query in loadedData.Descendants("person") select new Person { FirstName = (string)query.Element("firstname"), LastName = (string)query.Element("lastname"), Age = (int)query.Element("age") };listBox.ItemsSource = data;
在接下来的例子中,我们将通过数据的“年龄”属性值过滤。
源代码如下:
XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");var filteredData = from c in loadedCustomData.Descendants("Person") where c.Attribute("Age").Value == "27" select new Person() { FirstName = c.Attribute("FirstName").Value, LastName = c.Attribute("LastName").Value }; listBox1.ItemsSource = filteredData;
为了显示的数据,我们将使用以下ItemTemplates绑定ListBox控件:
WindowsPhone7,中的,XML,读取,、,过滤,以 上一篇:ipad暴风影音怎么播放多集电视剧、音画不同步怎么办? 下一篇:
C#中的索引器详细讲解
索引器类型于属性,它们的不同之处在于索引器的访问器采用参数。
定义了索引器之后,就可以像访问数组一样,使用[]运算符访问类的成员。
定义索引器的方式与定义属性有些类似,其一般形式如下:[修饰符] 数据类型 this {get{//获得属性的代码} set{//设置属性的代码}}索引器概述:使用索引器可以用类似于数组的方式为对象建立索引。
get accessor returns a value." data-guid="5e97def07fe15e931d526cafa5b97644">get 访问器返回值。
set accessor assigns a value." data-guid="66b9797a805b510e697d60c805140aeb">set 访问器分配值。
this keyword is used to define the indexers." data-guid="c34f23e68daf270013978542591de397">this 关键字用于定义索引器。
value keyword is used to define the value being assigned by the set indexer." data-guid="599578d2a780928307c04f6ca2b0f058">value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
索引器可被重载。
索引器可以有多个形参,例如当访问二维数组时。
属性与索引器的比较:属性 索引器允许像调用公共数据成员一样调用方法允许对一个对象本身使用数组表示法来访问该对象内部集合中的元素可通过简单的名称进行访问可通过索引器进行访问可以为静态成员或实例成员必须为实例成员get accessor of a property has no parameters." data-guid="a6cc8a328d7d6f37fe6726bef0a1e49b">属性的 get 访问器没有参数get accessor of an indexer has the same formal parameter list as the indexer." data-guid="d83df23e86b78dbdb4847d31d82a2337">索引器的 get 访问器具有与索引器相同的形参表set accessor of a property contains the implicit value parameter." data-guid="7b8d943b10ea3a65d99f8a3c0b284e05">属性的 set 访问器包含隐式 value 参数set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter." data-guid="61fa719a7490fc5fb284c6b5ef3c3c36">除了值参数外,索引器的 set 访问器还具有与索引器相同的形参表Auto-Implemented Properties (C# Programming Guide)." data-guid="0980783e25d81c644f0194104a5b78f1">支持对自动实现的属性(C# 编程指南)使用短语法不支持短语法get and set accessor methods as a means of assigning and retrieving values." data-guid="dd23dc3354d63d8f7d007da64625c804"> 在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的方法)。
Program class creates an instance of this class for storing strings." data-guid="6ddaa503e5fad577a85f10e9f502a5c1">Program 类为存储字符串创建了此类的一个实例。
class SampleCollection // Declare an array to store the data elements. private T[] arr = new T[100]; // Define the indexer, which will allow client code // to use [] notation on the class instance itself. // (See line 2 of code in Main below.) public T this[int i] // This indexer is very simple, and just returns or sets // the corresponding element from the internal array. return arr[i]; arr[i] = value;// This class shows how client code uses the indexer.class Program static void Main(string[] args) // Declare an instance of the SampleCollection type. SampleCollection stringCollection = new SampleCollection(); // Use [] notation on the type. stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]);}接口中的索引器:interface (C# Reference)." data-guid="9d8d9a82ccf82d740176b0fc9dbd9f72">索引器可在接口上声明。
class indexers in the following ways:" data-guid="6f6aa5a1638e327697f0ddb85ba767e0">接口索引器的访问器与类索引器的访问器具有以下方面的不同:接口访问器不使用修饰符。
接口访问器没有体。
因此,访问器的用途是指示索引器是读写、只读还是只写。
以下是接口索引器访问器的示例:public interface ISomeInterface{ //... // Indexer declaration: string this[int index] { get; set; }}一个索引器的签名必须区别于在同一接口中声明的其他所有索引器的签名。
下面的示例显示如何实现接口索引器。
// Indexer on an interface: public interface ISomeInterface { // Indexer declaration: int this[int index] { get; set; } } // Implementing the interface. class IndexerClass : ISomeInterface { private int[] arr = new int[100]; public int this[int index] // indexer declaration { get { // The arr object will throw IndexOutOfRange exception. return arr[index]; } set { arr[index] = value; } } } class MainClass { static void Main() { IndexerClass test = new IndexerClass(); System.Random rand = new System.Random(); // Call the indexer to initialize its elements. for (int i = 0; i 例如,如果 Employee 类实现的是两个接口 ICitizen 和 IEmployee,并且这两个接口具有相同的索引器签名,则必须使用显式接口成员实现。
即,以下索引器声明:public string IEmployee.this } 在 IEmployee 接口上实现索引器,而以下声明:public string ICitizen.this } 在 ICitizen 接口上实现索引器。
中的,索引,器,详细,讲解,索引,器,是,引入,的,
.net中的Debug与Trace的调试心得
不过里面介绍的一些调试的心得还不错,有些收获。
下面我就总结下本书中关于Debug和Trace的一些应用,算作一种读书笔记。
粗略概括下关于修改BUG的几种方法:通过调试器调试 顾名思义就是跟踪源代码进行调试,微软还提供了远程调试的功能。
通过源代码的方式调试,我们一般会采用以下的几种方法:使用Exception使用MessageBox。
使用#if Debug .. #endif使用断言调试:其中有Debug.Assert和Trace.Assert两种,Debug只在Debug模式下有效,Trace则都有效。
断言模式还有个优点,可以直接点击按钮重试,选择调试器进行调试。
当然还有通过堆栈跟踪或者其他的一些特殊手段等等。
.net所提供的断言调试相当强大,建议大家采用该方法。
具体应用请自行寻找资料。
msdn: Debug.Assert 方法 Trace.Assert 方法通过日志调试 想必大家肯定遇到过,有些BUG在客户那边会出现,但是本机就是模拟不出来。
而客户要嘛远隔万里,要嘛就是不允许安装远程调试。
在这种情况下,就可以通过日志调试,也就是将一些重要的信息写到文本中,当出现问题或者BUG可以通过查看日志文件来查找原因。
记录日志文件,就是写文件的操作,可以有很多种方法。
此处介绍下通过.net提供的侦听器与开关记录日志。
1.直接创建1 static void Main(string[] args)2 {3 bool someBool = true;4 Trace.Listeners.Add(new TextWriterTraceListener(@"C:\MyListener.log")); 5 Trace.AutoFlush = true;//每次写入日志后是否都将其保存到磁盘中6 7 Trace.WriteLine(DateTime.Now.ToString() + "--Enter function LogTest");8 Trace.Indent(); //缩进+19 Trace.WriteLine("This is indented once");10 Trace.Indent();11 Trace.WriteLineIf(someBool, "Only written if someBool is true");12 Trace.Unindent(); //缩进-113 Trace.Unindent();14 Trace.WriteLine("Leave function LogTest");15 }日志内容:2012-05-30 14:11:41--Enter function LogTest This is indented once Only written if someBool is trueLeave function LogTest 2.通过配置文件创建App.config1 static void Main(string[] args)2 {3 TraceSwitch ts = new TraceSwitch("MySwitch", "descrption");//开关4 Trace.WriteLine(DateTime.Now.ToString() + "--Enter function LogTest");5 Trace.Indent(); //缩进+16 Trace.WriteLine("This is indented once");7 Trace.Indent();8 Trace.WriteLineIf(ts.TraceInfo, "MySwitch TraceInfo is true!");9 Trace.Unindent(); //缩进-110 Trace.Unindent();11 Trace.WriteLine("Leave function LogTest");12 }日志内容:2012-05-30 14:19:43--Enter function LogTest This is indented once MySwitch TraceInfo is true!Leave function LogTest 3.windows事件日志 通过Trace或者Debug中的EventLogTraceListener即可写入事件日志。
.net,中的,Debug,与,Trace,的,调试,心得,