Weak Event seems to fail in test method
-
EDIT this was an accidental post, problem is fixed!! I am running / fixing all my unit test for my home made utilities. I have a weak event build method which look like that
public static PropertyChangedEventHandler AddWeakHandler<T>(this INotifyPropertyChanged model, T target, Action<T, PropertyChangedEventArgs> action) where T : class { var weakRef = new WeakReference(target); PropertyChangedEventHandler handler = null; handler = new PropertyChangedEventHandler( (s, e) => { var strongRef = weakRef.Target as T; if (strongRef != null) { action(strongRef, e); } else { model.PropertyChanged -= handler; handler = null; } }); model.PropertyChanged += handler; return handler; }
it is used like that (to show that there are no captured variable in the lambda expression
public object Source { get { return mSource; } set { var p = Property; if (p == null) value = null; if (value == mSource) return; if (Source is INotifyPropertyChanged) { ((INotifyPropertyChanged)Source).PropertyChanged -= previous; previous = null; } mSource = value; if (Source is INotifyPropertyChanged) { previous = **WeakEvents.AddWeakHandler**((INotifyPropertyChanged)Source, this, (x, arg) => x.OnSourcePropertyChanged(Source, arg)); } if (p != null) { p.OnPropertyChanged(); } } }
I have a simple (XUnit) test looking like that
\[Fact\] public void CheckPropertyPathIsWeakEvent() { var m = new ModelForPathCheck(); int n = 0; Action<string
-
EDIT this was an accidental post, problem is fixed!! I am running / fixing all my unit test for my home made utilities. I have a weak event build method which look like that
public static PropertyChangedEventHandler AddWeakHandler<T>(this INotifyPropertyChanged model, T target, Action<T, PropertyChangedEventArgs> action) where T : class { var weakRef = new WeakReference(target); PropertyChangedEventHandler handler = null; handler = new PropertyChangedEventHandler( (s, e) => { var strongRef = weakRef.Target as T; if (strongRef != null) { action(strongRef, e); } else { model.PropertyChanged -= handler; handler = null; } }); model.PropertyChanged += handler; return handler; }
it is used like that (to show that there are no captured variable in the lambda expression
public object Source { get { return mSource; } set { var p = Property; if (p == null) value = null; if (value == mSource) return; if (Source is INotifyPropertyChanged) { ((INotifyPropertyChanged)Source).PropertyChanged -= previous; previous = null; } mSource = value; if (Source is INotifyPropertyChanged) { previous = **WeakEvents.AddWeakHandler**((INotifyPropertyChanged)Source, this, (x, arg) => x.OnSourcePropertyChanged(Source, arg)); } if (p != null) { p.OnPropertyChanged(); } } }
I have a simple (XUnit) test looking like that
\[Fact\] public void CheckPropertyPathIsWeakEvent() { var m = new ModelForPathCheck(); int n = 0; Action<string
I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.
-
I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.
My bad... I should not have posted that!!! Garbage collection worked fine (as test finalizer indicated) but the event was spuriously triggered in the PropertyPath constructor! all solved! ^^
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.
on a side note I didn't understand your confusion! ;P
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.
full source code, for your understanding!
public class PropertyPath<TProp> : PropertyPath
{
internal PropertyPath()
{
}
public new TProp Value
{
get
{
var v = base.Value;
if (v is TProp)
return (TProp)v;
return default(TProp);
}
set { base.Value = value; }
}
}
public class PropertyPath : ModelBase
{
public static PropertyPath<TP> Link<T, TP>(T root, Expression<Func<T, TP>> e, Action<TP> onValueChanged)
{
var pv = new PropertyPath<TP>();
pv.PropertyChanged += (o, eargs) =>
{
if (string.IsNullOrEmpty(eargs.PropertyName) || eargs.PropertyName == "Value")
{
onValueChanged(pv.Value);
}
};
pv.Initialize(e);
pv.Root = root;
return pv;
}
public static PropertyPath<TP> Link<TP>(Expression<Func<TP>> e, Action<TP> onValueChanged)
{
var pv = new PropertyPath<TP>();
pv.PropertyChanged += (o, eargs) =>
{
if (string.IsNullOrEmpty(eargs.PropertyName) || eargs.PropertyName == "Value")
{
onValueChanged(pv.Value);
}
};
pv.Initialize(e);
return pv;
}public static PropertyPath<TP> Create<T, TP>(T root, Expression<Func<T, TP>> e) { var pv = new PropertyPath<TP>(); pv.Initialize(e); pv.Root = root; return pv; } public static PropertyPath<TP> Create<TP>(Expression<Func<TP>> e) { var pv = new PropertyPath<TP>(); pv.Initialize(e); return pv; } internal PropertyPath() { } void Initialize(LambdaExpression e) { object root; var path = ReflectionEx.GetLambdaPath(e, out root); if (path.Length == 0) throw new ArgumentException(); pvalues = new PropertyValue\[path.Length\]; roValues = new ReadOnlyCollection<PropertyValue>(pvalues); for (int i = 0
-
I'm confused. The notification is raised (and usually collected) at the class level. There should be no point in changing the INotifyPreoprtyChanged, but whatever floats your boat I guess.
What you might have misread... the class is NOT registering a listener to its own change! It's listening to change in the source object (!= this!) !!
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
What you might have misread... the class is NOT registering a listener to its own change! It's listening to change in the source object (!= this!) !!
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
No worries, I just saw the little code snippet and I don't know what your requirements are. My knee jerk reaction was that there has to be a simpler way of doing this. Btw, there is a standard WeakEventManager inside .NET (first intoduced in 3.0) WeakEventManager Class (System.Windows)[^]
-
No worries, I just saw the little code snippet and I don't know what your requirements are. My knee jerk reaction was that there has to be a simpler way of doing this. Btw, there is a standard WeakEventManager inside .NET (first intoduced in 3.0) WeakEventManager Class (System.Windows)[^]
It's not supported by the PCL!!! But no worries, it's simple enough to implement that I made my own PCL version! :-D
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
It's not supported by the PCL!!! But no worries, it's simple enough to implement that I made my own PCL version! :-D
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
PCL?
-
PCL?
Cross-Platform Development with the Portable Class Library[^] I.e. PCL class library support normal .NET (4.5), UWP, Xamarin iOS, Xamarin Android, and the upcoming .NET Core / .NET native!
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!