win phone 7第一个程序(续)
时间:2010-12-09 来源:helloxyz
(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}}"
相关阅读 更多 +