Skip to content

Commit 8000048

Browse files
Fix for MenuFlyoutItem stops working after navigating away from and back to page (#25170)
* fix-21675-Updated the BindingContext of MenuBarItem after page navigation. * fix-21675- Added UI testcase. * fix-21675-Removed unwanted space. * fix-21675-removed unwanted spacing. * fix-21675-Updated testcase. * fix-21675-Updated the BindingContext of MenuBarItem after page navigation. * fix-21675- Added UI testcase. * fix-21675-Removed unwanted space. * fix-21675-removed unwanted spacing. * fix-21675-Updated testcase. * fix-21675-Updated testcase script. * fix-21675-Updated testcase which only execute for the Windows and MacOS * fix-21675-Moved testcase classes to Issue21675. * Update src/Controls/src/Core/Page/Page.cs * fix-21675-Added UnitTestcase and removed UI testcase. * fix-21675- Parent of the MenuBarItems only null when the parent is MenuBar instead of page. --------- Co-authored-by: Rui Marinho <[email protected]>
1 parent c205c84 commit 8000048

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/Controls/src/Core/Menu/MenuBar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void RemoveAt(int index)
113113
_menus.RemoveAt(index);
114114
NotifyHandler(nameof(IMenuBarHandler.Remove), index, item);
115115

116-
if (item is Element e)
116+
if (item is Element e && e.Parent == this)
117117
e.Parent = null;
118118
}
119119

src/Controls/tests/Core.UnitTests/Menu/MenuTestBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,17 @@ public void ClearUpdatesParent()
137137
if (typeof(TChildType) == typeof(MenuBarItem))
138138
{
139139
(child0.Parent as ContentPage)!.MenuBarItems.Clear();
140+
141+
Assert.NotNull(child0.Parent);
142+
Assert.NotNull(child1.Parent);
140143
}
141144
else
142145
{
143146
menuBar.Clear();
144-
}
145147

146-
Assert.Null(child0.Parent);
147-
Assert.Null(child1.Parent);
148+
Assert.Null(child0.Parent);
149+
Assert.Null(child1.Parent);
150+
}
148151
}
149152

150153
[Fact]

src/Controls/tests/Core.UnitTests/MenuItemTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,46 @@ public void PartialHierarchyCanBeDisabled()
192192
Assert.False(bottomLevelItem1.IsEnabled);
193193
Assert.False(bottomLevelItem2.IsEnabled);
194194
}
195+
196+
[Fact]
197+
public async void UpdateMenuBarItemBindingContext()
198+
{
199+
bool fired = false;
200+
var cmd = new Command(() => fired = true);
201+
var bindingContext = new
202+
{
203+
MenuItemCommand = cmd
204+
};
205+
206+
MenuFlyoutItem flyout;
207+
MenuBarItem menuItem;
208+
var mainPage = new ContentPage
209+
{
210+
MenuBarItems =
211+
{
212+
(menuItem = new MenuBarItem
213+
{
214+
(flyout = new MenuFlyoutItem { })
215+
})
216+
}
217+
};
218+
219+
mainPage.BindingContext = bindingContext;
220+
flyout.SetBinding(MenuFlyoutItem.CommandProperty, new Binding("MenuItemCommand"));
221+
222+
var page = new ContentPage
223+
{
224+
BindingContext = bindingContext
225+
};
226+
227+
NavigationPage nav = new NavigationPage(mainPage);
228+
TestWindow testWindow = new TestWindow(nav);
229+
await mainPage.Navigation.PushAsync(page);
230+
await page.Navigation.PopAsync();
231+
232+
flyout.Command.Execute(null);
233+
234+
Assert.True(fired);
235+
}
195236
}
196237
}

0 commit comments

Comments
 (0)