public static Message[] GetChanges(MessageBridge bridge, Action localAction) { var changes = new List <Message>(); ChangeMessageEvent chEvent = (_, changeMsg) => changes.Add(changeMsg); bridge.ChangeMessageEvent += chEvent; localAction(); bridge.ChangeMessageEvent -= chEvent; return(changes.ToArray()); }
public MessageBridge() { ObjectContext.ObjectPropertyChanged += (objContext, liveObj, propName) => { if (silencedProps.IsSilenced(liveObj, propName)) { return; } var objectDesc = (ObjectDescriptor)ObjectContext.TypeContext.GetTypeDescriptor(liveObj.GetType(), false); var newValue = objectDesc.Properties[propName].PropertyInfo.GetValue(liveObj); ChangeMessageEvent?.Invoke(this, new Message { Error = MessageError.NoError, MessageType = MessageType.PropertyChanged, ResourceId = liveObj.ResourceId, PropertyName = propName, Value = newValue }); }; ObjectContext.ListChanged += (objContext, liveObj, propDesc, changeArgs) => { var propName = propDesc.PropertyInfo.Name; if (silencedProps.IsSilenced(liveObj, propName)) { return; } var isReset = changeArgs.Action == NotifyCollectionChangedAction.Reset; var changeMsg = new Message { Error = MessageError.NoError, MessageType = isReset ? MessageType.PropertyChanged : MessageType.ListChanged, ResourceId = liveObj.ResourceId, PropertyName = propName, ListChanges = isReset ? null : new List <ListChangeItem>() }; // Action = Add, NewStartingIndex = 0, OldStartingIndex = -1, NewItems = ["inserted item #0"] // Action = Add, NewStartingIndex = 5, OldStartingIndex = -1, NewItems = ["added item"] // Action = Remove, NewStartingIndex = -1, OldStartingIndex = 0, NewItems = null, OldItems = ["ListItem #1"] // Action = Move, NewStartingIndex = 1, OldStartingIndex = 0, NewItems = ["inserted item #0"], OldItems = ["inserted item #0"] // Action = Replace, NewStartingIndex = 0, OldStartingIndex = 0, NewItems = ["new value"], OldItems = ["ListItem #2"] for (var i = 0; i < changeArgs.OldItems?.Count; i++) { changeMsg.ListChanges.Add(new ListChangeItem { Action = ListChangeAction.Remove, Index = changeArgs.OldStartingIndex, Value = changeArgs.OldItems[i] }); } for (var i = 0; i < changeArgs.NewItems?.Count; i++) { changeMsg.ListChanges.Add(new ListChangeItem { Action = ListChangeAction.Add, Index = changeArgs.NewStartingIndex + i, Value = changeArgs.NewItems[i] }); } ChangeMessageEvent?.Invoke(this, changeMsg); }; }