Skip to content

Commit ce296f9

Browse files
authored
Merge pull request #3429 from Sergio0694/feature/mvvm-preview2
[Feature] Microsoft.Toolkit.Mvvm package (Preview 2)
2 parents 130dbe0 + fec4b22 commit ce296f9

File tree

13 files changed

+913
-432
lines changed

13 files changed

+913
-432
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableObject.cs

Lines changed: 212 additions & 104 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableRecipient.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using System;
1111
using System.Collections.Generic;
12-
using System.Linq.Expressions;
1312
using System.Runtime.CompilerServices;
1413
using Microsoft.Toolkit.Mvvm.Messaging;
1514
using Microsoft.Toolkit.Mvvm.Messaging.Messages;
@@ -204,7 +203,14 @@ protected bool SetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comp
204203
/// </remarks>
205204
protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, bool broadcast, [CallerMemberName] string? propertyName = null)
206205
{
207-
return SetProperty(oldValue, newValue, EqualityComparer<T>.Default, callback, broadcast, propertyName);
206+
bool propertyChanged = SetProperty(oldValue, newValue, callback, propertyName);
207+
208+
if (propertyChanged && broadcast)
209+
{
210+
Broadcast(oldValue, newValue, propertyName);
211+
}
212+
213+
return propertyChanged;
208214
}
209215

210216
/// <summary>
@@ -237,40 +243,53 @@ protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> compa
237243
/// Compares the current and new values for a given nested property. If the value has changed,
238244
/// raises the <see cref="ObservableObject.PropertyChanging"/> event, updates the property and then raises the
239245
/// <see cref="ObservableObject.PropertyChanged"/> event. The behavior mirrors that of
240-
/// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,string)"/>, with the difference being that this
246+
/// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string)"/>, with the difference being that this
241247
/// method is used to relay properties from a wrapped model in the current instance. For more info, see the docs for
242-
/// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,string)"/>.
248+
/// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string)"/>.
243249
/// </summary>
244-
/// <typeparam name="T">The type of property to set.</typeparam>
245-
/// <param name="propertyExpression">An <see cref="Expression{TDelegate}"/> returning the property to update.</param>
250+
/// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam>
251+
/// <typeparam name="T">The type of property (or field) to set.</typeparam>
252+
/// <param name="oldValue">The current property value.</param>
246253
/// <param name="newValue">The property's value after the change occurred.</param>
254+
/// <param name="model">The model </param>
255+
/// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param>
247256
/// <param name="broadcast">If <see langword="true"/>, <see cref="Broadcast{T}"/> will also be invoked.</param>
248257
/// <param name="propertyName">(optional) The name of the property that changed.</param>
249258
/// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
250-
protected bool SetProperty<T>(Expression<Func<T>> propertyExpression, T newValue, bool broadcast, [CallerMemberName] string? propertyName = null)
259+
protected bool SetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, bool broadcast, [CallerMemberName] string? propertyName = null)
251260
{
252-
return SetProperty(propertyExpression, newValue, EqualityComparer<T>.Default, broadcast, propertyName);
261+
bool propertyChanged = SetProperty(oldValue, newValue, model, callback, propertyName);
262+
263+
if (propertyChanged && broadcast)
264+
{
265+
Broadcast(oldValue, newValue, propertyName);
266+
}
267+
268+
return propertyChanged;
253269
}
254270

255271
/// <summary>
256272
/// Compares the current and new values for a given nested property. If the value has changed,
257273
/// raises the <see cref="ObservableObject.PropertyChanging"/> event, updates the property and then raises the
258274
/// <see cref="ObservableObject.PropertyChanged"/> event. The behavior mirrors that of
259-
/// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,IEqualityComparer{T},string)"/>,
275+
/// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,IEqualityComparer{T},TModel,Action{TModel,T},string)"/>,
260276
/// with the difference being that this method is used to relay properties from a wrapped model in the
261277
/// current instance. For more info, see the docs for
262-
/// <see cref="ObservableObject.SetProperty{T}(Expression{Func{T}},T,IEqualityComparer{T},string)"/>.
278+
/// <see cref="ObservableObject.SetProperty{TModel,T}(T,T,IEqualityComparer{T},TModel,Action{TModel,T},string)"/>.
263279
/// </summary>
264-
/// <typeparam name="T">The type of property to set.</typeparam>
265-
/// <param name="propertyExpression">An <see cref="Expression{TDelegate}"/> returning the property to update.</param>
280+
/// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam>
281+
/// <typeparam name="T">The type of property (or field) to set.</typeparam>
282+
/// <param name="oldValue">The current property value.</param>
266283
/// <param name="newValue">The property's value after the change occurred.</param>
267284
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use to compare the input values.</param>
285+
/// <param name="model">The model </param>
286+
/// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param>
268287
/// <param name="broadcast">If <see langword="true"/>, <see cref="Broadcast{T}"/> will also be invoked.</param>
269288
/// <param name="propertyName">(optional) The name of the property that changed.</param>
270289
/// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
271-
protected bool SetProperty<T>(Expression<Func<T>> propertyExpression, T newValue, IEqualityComparer<T> comparer, bool broadcast, [CallerMemberName] string? propertyName = null)
290+
protected bool SetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, bool broadcast, [CallerMemberName] string? propertyName = null)
272291
{
273-
bool propertyChanged = SetProperty(propertyExpression, newValue, comparer, out T oldValue, propertyName);
292+
bool propertyChanged = SetProperty(oldValue, newValue, comparer, model, callback, propertyName);
274293

275294
if (propertyChanged && broadcast)
276295
{

0 commit comments

Comments
 (0)