引言
在B/S开发中客户端常用到Silverlight,为了使前、后台能够共享类,采用WCF方式从后台传递数据至前台。当在Silverlight中通过DataGrid绑定显示数据时,需要为DataGrid指定从服务器端获取的数据源,同时还要为DataGrid对应的Column指定对应的字段。当修改服务器端类的属性后,会出现前台绑定的字段无法显示的问题。
问题的分析
在Silverlight中如何通过WCF方式从后台传递数据至前台,并在前、后台共享类请参考
http://www.cnblogs.com/jv9/archive/2009/12/04/1616667.html
针对修改服务器端类中属性,前台对应字段无法显示问题,单步执行程序至为DataGrid指定数据源处,发现数据源中类的属性仍旧是修改前的属性。继续跟踪,程序出现错误并跳转至reference.cs文件中。
实际上,Silverlight为每一个WCF服务都创建了一个reference.cs类,该类中包含了WCF服务与Silverlight的交互信息,并且在该类中为每一个在当前WCF服务中引用的类创建相应的转换类,类的对应信息如下:
服务器端类
public class RescueEquipInfo { public string str_Name { get; set; } public string str_MeasureUnit { get; set; } public string str_Model { get; set; } public string str_Type { get; set; } public double d_Count { get; set; } public string str_Unit { get; set; } } |
Reference.cs中对应的类描述:
[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.DataContractAttribute(Name="RescueEquipInfo", Namespace="http://schemas.datacontract.org/2004/07/AHKY.Model.Entities")] public partial class RescueEquipInfo : object, System.ComponentModel.INotifyPropertyChanged { private double d_CountField; private string str_MeasureUnitField; private string str_ModelField; private string str_NameField; private string str_TypeField; private string str_UnitField; [System.Runtime.Serialization.DataMemberAttribute()] public double d_Count { get { return this.d_CountField; } set { if ((this.d_CountField.Equals(value) != true)) { this.d_CountField = value; this.RaisePropertyChanged("d_Count"); } } }
[System.Runtime.Serialization.DataMemberAttribute()] public string str_MeasureUnit { get { return this.str_MeasureUnitField; } set { if ((object.ReferenceEquals(this.str_MeasureUnitField, value) != true)) { this.str_MeasureUnitField = value; this.RaisePropertyChanged("str_MeasureUnit"); } } }
[System.Runtime.Serialization.DataMemberAttribute()] public string str_Model { get { return this.str_ModelField; } set { if ((object.ReferenceEquals(this.str_ModelField, value) != true)) { this.str_ModelField = value; this.RaisePropertyChanged("str_Model"); } } }
[System.Runtime.Serialization.DataMemberAttribute()] public string str_Name { get { return this.str_NameField; } set { if ((object.ReferenceEquals(this.str_NameField, value) != true)) { this.str_NameField = value; this.RaisePropertyChanged("str_Name"); } } }
[System.Runtime.Serialization.DataMemberAttribute()] public string str_Type { get { return this.str_TypeField; } set { if ((object.ReferenceEquals(this.str_TypeField, value) != true)) { this.str_TypeField = value; this.RaisePropertyChanged("str_Type"); } } }
[System.Runtime.Serialization.DataMemberAttribute()] public string str_Unit { get { return this.str_UnitField; } set { if ((object.ReferenceEquals(this.str_UnitField, value) != true)) { this.str_UnitField = value; this.RaisePropertyChanged("str_Unit"); } } }
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) { System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if ((propertyChanged != null)) { propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); } } } |
当修改服务器端类的属性后,在reference.cs类中,相应属性的对应关系没有得到更新,仍旧对应的修改前的属性名称。为解决本问题,需要将reference.cs中对应关系的属性名称修改为服务器端修改后的属性名称。