-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
What steps will reproduce this issue?
I got a Null Reference exception when trying to open my Metro app that contains several metro windows. I really don't have a particular way to re-create this as I typically don't have this error. I'm guessing this is dependent on the execution order of the underlying threads somehow. The fix looks simple I guess, but I'm not sure how to create a test for it (assuming that's required). Here's my stack trace.
System.NullReferenceException: Object reference not set to an instance of an object.
at MahApps.Metro.Behaviours.BorderlessWindowBehavior.TopMostChangeNotifierOnValueChanged(Object sender, EventArgs e)
at MahApps.Metro.Controls.PropertyChangeNotifier.OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.OnDependencyPropertyChanged(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.Window.set_Topmost(Boolean value)
at System.Windows.Window.OnContentRendered(EventArgs e)
at System.Windows.Window.b__193_0(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
Expected outcome
Um, it doesn't crash?
Suggested Code Change
Here's the existing code. The only thing that could be null is the AssociatedObject.
private void TopMostChangeNotifierOnValueChanged(object sender, EventArgs e)
{
this.savedTopMost = this.AssociatedObject.Topmost;
}
So, I think the obvious change should be this, but beyond that, I'm not sure what should happen.
private void TopMostChangeNotifierOnValueChanged(object sender, EventArgs e)
{
if (this.AssociatedObject != null)
this.savedTopMost = this.AssociatedObject.Topmost;
}
Environment
- MahApps.Metro v1.3.0
- Windows 7.1
- Visual Studio 2015
- .NET Framework 4.5