Browse Source

Merge pull request #3747 from AvaloniaUI/fixes/contextmenu-nre

Fix ContextMenu NullReferenceException
release/0.9.8
Steven Kirk 6 years ago
committed by Dan Walmsley
parent
commit
2eabb99f83
  1. 2
      src/Avalonia.Controls/ContextMenu.cs
  2. 17
      tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

2
src/Avalonia.Controls/ContextMenu.cs

@ -75,7 +75,7 @@ namespace Avalonia.Controls
{ {
control.PointerReleased -= ControlPointerReleased; control.PointerReleased -= ControlPointerReleased;
oldMenu._attachedControl = null; oldMenu._attachedControl = null;
((ISetLogicalParent)oldMenu._popup).SetParent(null); ((ISetLogicalParent)oldMenu._popup)?.SetParent(null);
} }
if (e.NewValue is ContextMenu newMenu) if (e.NewValue is ContextMenu newMenu)

17
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

@ -1,9 +1,5 @@
using System; using System;
using System.Windows.Input;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Markup.Data;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Moq; using Moq;
@ -159,6 +155,19 @@ namespace Avalonia.Controls.UnitTests
} }
} }
[Fact]
public void Can_Set_Clear_ContextMenu_Property()
{
using (Application())
{
var target = new ContextMenu();
var control = new Panel();
control.ContextMenu = target;
control.ContextMenu = null;
}
}
[Fact(Skip = "The only reason this test was 'passing' before was that the author forgot to call Window.ApplyTemplate()")] [Fact(Skip = "The only reason this test was 'passing' before was that the author forgot to call Window.ApplyTemplate()")]
public void Cancelling_Closing_Leaves_ContextMenuOpen() public void Cancelling_Closing_Leaves_ContextMenuOpen()
{ {

Loading…
Cancel
Save