Search code examples
silverlightwcf-ria-servicesinotifypropertychanged

Silverlight RIA: How do I implement change notification on my shared code


I have extended my domain service using a "shared" code file to add an additional property to my "Booking" class that calculates mileage from StartMileage and FinishMileage as follows:

 public int? JourneyMileage
        {
            get
            {
                if (StartMileage.HasValue && FinishMileage.HasValue)
                {
                    return (FinishMileage - StartMileage);
                }
                else
                {
                    return null;
                }
            }
        }

StartMileage and FinishMileage are properties in my autogenerated domain service. The property JourneyMileage is bound to the UI, as are StartMileage and FinishMileage.

How do I update the JourneyMileage property (and therefore the UI as well) when StartMileage or FinishMileage are changed? I have been looking for something like NotifyPropertyChanged but seem to have drawn a blank.


Solution

  • Add to your shared Booking class a conditional silverlight code block like this.

    #if SILVERLIGHT
      partial void OnStartMileageChanged(){
        RaisePropertyChanged("JourneyMileage");
      }
    
      partial void OnFinishMileageChanged(){
        RaisePropertyChanged("JourneyMileage");
      }
    
    #endif
    

    This should work. Don´t tested it.

    Another way is, if you don´t need the JourneyMileage property on the server side, to create a partial "Booking" class on the client side, define your property and put my code without the conditional statement in the partial class.