win phone 7第一个程序(续)
昨天做了自己的第一个win phone 7程序,今天继续这个程序,进行一些改进。邮编查询小工具,不需要通过点击button按钮来查询地址,即输入完就自动显示查询结果。
(1)第一步要做的是建一个类,命名为PostcodeClass.cs,主要代码如下:
public class PostcodeClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int postcode;
public int Postcode
{
get
{
return postcode;
}
set
{
postcode = value;
address = PostCodeQuery.Instance.GetAddressByPostcode(postcode);
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Address"));
}
}
}
private string address = "没找到";
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}
}
(2)然后的工作是如何引用新建类中的类。导入名字空间xmlns:local="clr-namespace:firsttext"
<phone:PhoneApplicationPage.Resources>
<local:PostcodeClass x:Key="PostcodeClass"></local:PostcodeClass>
</phone:PhoneApplicationPage.Resources>这段代码作用不清楚
(3)控件进行绑定,可以用视图操作,代码如下:
Text="{Binding Path=Postcode,Mode=TwoWay,Source={StaticResource PostcodeClass}}"
Text="{Binding Path=Address,Mode=OneWay,Source={StaticResource PostcodeClass}}"