【问题标题】:Using class from wcf service in silverlight - notifypropertychanged doesn't work在 silverlight 中使用 wcf 服务中的类 - notifypropertychanged 不起作用
【发布时间】:2026-01-29 04:50:02
【问题描述】:

我的 wcf 服务中定义了以下类

public class Autoturism : INotifyPropertyChanged
    {
        private int _AutoturismID;
        public int AutoturismID
        { get { return _AutoturismID; } set { _AutoturismID = value; NotifyPropertyChanged("AutoturismID"); } }

        private string _NumarAutoturism;
        public string NumarAutoturism
        { get { return _NumarAutoturism; } set { _NumarAutoturism = value; NotifyPropertyChanged("NumarAutoturism"); } }

        public bool IsDirty { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            IsDirty = true;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

我想使用 IsDirty 值来检查对象是否需要保存在数据库中。

在 silverlight 页面中,我有以下几行:

AutoCurent = new Autoturism();
AutoCurent.NumarAutoturism="13424";

我的问题是,在最后一行之后,我期望 IsDirty= true 但它仍然是错误的。我认为来自服务引用的 Auoturism 类不再具有 NotifyPropertyChanged 方法。

我做错了什么? 谢谢

【问题讨论】:

  • +1 提供大量代码和良好的描述:)

标签: c# silverlight wcf inotifypropertychanged


【解决方案1】:

如果您使用的是 RIA WCF 服务(我必须从您的问题中假设),那么您的 Autoturism 对象的客户端版本将只有属性,而不是代码,来自您的服务端类。

基本上,RIA 服务为客户端创建仅具有相同“形状”的代理对象(而不是代码)。

要完全共享代码和数据,您需要将它们作为 .shared.cs 类。然后将完整的源代码复制到您的客户项目中。

【讨论】:

  • 谢谢你,我害怕那个。我要搜索一些关于共享课程的信息