开发者

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 additio开发者_StackOverflow社区nal 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.


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.


You do NOT update the JourneyMilage property. It changes when the other fields change.

If you want to notify others that its value has changed, implement INotifyPropertyChanged and raise the PropertyChanged event for JourneyMilage too when either StartMilage or FinishMilage change.

EDIT

See this post


This should work. Don´t tested it.

1) This won't be compiled with Silverlight, if you need property changed notification. The generated shared class on the client side is from another namespace (other .dll in SL) and has other method signatures for raising property changed :-(

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.

2) This will work and could be a reasonable solution, however you can't then share the business logic with the EDM :-(

3) One other solution could be to add the property to generated metadata class with [DataMember] attribute. The disadvantage (or in some use cases advantage) is that changing such property will set context as changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜