如果服务器端的集合已更改(通过客户端操作或服务器操作),我从几天以来一直在尝试将 PropertyChanged 事件从我的 wcf 服务获取到 wcf 客户端。必须有更好的解决方案,而不是使用回调并重新加载列表......或者?
在服务器端:(几乎就像另一篇文章中的示例) ObservableCollection 和 CollectionChanged 事件作为 WCF 数据合同
public interface IObservableService
{
[OperationContract(IsOneWay = false)]
Data getData();
}
[DataContract]
public class Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("Notify()");
}
}
private ObservableCollection<string> list;
internal Data()
{
list = new ObservableCollection<string>();
list.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(list_CollectionChanged);
}
void list_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("list_CollectionChanged");
Notify("DataList");
Notify("Data");
}
[DataMember]
public ObservableCollection<string> DataList
{
get
{
return list;
}
set {
list = value;
Console.WriteLine("set DataList");
Notify("DataList");
Notify("Data");
}
}
}
在客户端:
ObservableServiceClient client = new ObservableServiceClient();
Data data = client.getData();
到目前为止它的工作......我可以在客户端查询集合,但是当服务器集合发生变化时我没有收到“propertyChanged”?
怎么了?我的错误和误解在哪里?