views:

72

answers:

1

I want a class (called PremiseServer) in my Mvvm-Light solution (WP7) to subscribe to all property changes for classes that are derived from a base type (SysObject is the base class and it derives from ViewModel).

I have a set of classes derived from SysObject. These classes have various properties on them of differing types (Strings, booleans, ints etc...).

When any property on any of these classes changes I want my PremiseServer instance to see these changes and then make web-service calls to push the data to a server.

I have tried this and it never gets called (which makes sense to me now; because the property that is getting changed is not SysObject, but some property OF the SysObject):

Messenger.Default.Register<PropertyChangedMessage<SysObject>>(this, (action) => {
    String location = ((SysObject)action.Sender).Location;  // URL to POST to
    Debug.WriteLine("PremiseServer PropertyChange - " + action.NewValue.ToString());
});

I also tried the below (registering String messages) and it works, but I don't want to create one of these for each property type:

Messenger.Default.Register<PropertyChangedMessage<String>>(this, (action) => {
    String location = ((SysObject)action.Sender).Location;  // URL to POST to
    Debug.WriteLine("PremiseServer PropertyChange - " + action.NewValue.ToString());
});

I also tried Register<PropertyChangeMessage<Object> thinking I'd see messages for all derived types (I didn't).

What I really want is "Register for all property change messags from any property of objects of class SysObject". How can I do that?

Thanks!

+3  A: 

Hi,

You can register for PropertyChangedMessageBase using the Register method overload that has a boolean flag as the last parameter, and setting this flag to true. Like its name shows, this flag allows you to register for a message type, or all messages deriving from this type.

Note that in the handler, you will need to cast the message to the exact type that you want to handle.

Does it make sense? Cheers, Laurent

LBugnion
Yep. Makes total sense and works as expected. Thanks!
cek