-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for MenuFlyoutItem stops working after navigating away from and back to page #25170
Conversation
Hey there @BagavathiPerumal! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
/rebase |
8d7eea9
to
cba919e
Compare
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
/rebase |
cba919e
to
4c04284
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like this needs a UITest
Can you move this to a unit test?
public class MenuItemTests |
@PureWeen, we couldn't recreate the same UI test scenario in the unit test case because the command of the MenuFlyoutItem is always present after page navigation.
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we validate this one using a unit test vs a UI Test?
src/Controls/src/Core/Page/Page.cs
Outdated
{ | ||
if (menuBarItem.BindingContext is null) | ||
{ | ||
SetInheritedBindingContext(menuBarItem, BindingContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this is where the actual bug is @BagavathiPerumal
The fix here might be to check if the Parent
is MenuBar
and if it's not then don't set it to null.
The Parent of MenuBarItem
is the Page that it's on so the Page
needs to control when the parent is set to null
@PureWeen, As suggested, when wiring up through a TestWindow in the UnitTest case, we couldn't recreate the UI test scenario because the MenuFlyoutItem command remains active after page navigation. Could you please share your suggestions.
|
@BagavathiPerumal in your test you aren't assigning the right thing to the window You need to change those lines to this NavigationPage nav = new NavigationPage(mainPage);
TestWindow testWindow = new TestWindow(nav);
await mainPage.Navigation.PushAsync(page);
await page.Navigation.PopAsync();
|
/rebase |
@PureWeen, Thank you for the suggestion. I have added the unit test case accordingly. Please review and let me know if you have any further concerns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment here still applies to the PR in its current state
Hi @PureWeen, Could you kindly check this response? We have attempted this approach and encountered a related test failure. |
/rebase |
@BagavathiPerumal AFAICT this code is wrong maui/src/Controls/src/Core/Menu/MenuBar.cs Lines 116 to 117 in 2478901
and should be changed to this if (item is Element e && e.Parent == this)
e.Parent = null; What I'm thinking here is
And then that should be the proper fix here. If I'm still missing something here can you summarize it all in a comment so it's a bit easier to follow where I'm not understanding? |
@PureWeen, As suggested, setting the parent of a MenuBarItem to null when its parent is MenuBar resolves the actual issue. However, it causes the following test case to fail. In this test case, the ContentPage's MenuBarItems are cleared. At that time, the parent of the MenuBarItem is ContentPage. Therefore, when MenuBarItems are cleared, the parent of the MenuBarItem must also be cleared and set to null. This seems to be a valid case. However, due to your suggested fix, these test cases have failed. The actual issue is that the MenuBarItem's BindingContext is reset to null after navigating to another page due to the previous page's MenuBarItem parent being set to null. To resolve this, I added a fix where the BindingContext for the MenuBarItem is reapplied within the OnAppearing() method of the page. This ensures that the MenuBarItem's BindingContext is correctly set when the page is displayed again after navigation, without affecting any previous logic. |
Yea, but the problem is that the The tests inside ClearUpdatesParent() attempt to be generalized across all the different types of menu since they apply to a lot of the same scenarios but there are cases where the logic differs. For example, if you check this test maui/src/Controls/tests/Core.UnitTests/Menu/MenuTestBase.cs Lines 94 to 100 in a5553ad
you'll see the logic deviates based on the type of item, so I'm pretty sure this is the same case. The |
…nuBar instead of page.
@PureWeen, As suggested, I have modified the fix and the test case(ClearUpdatesParent()). Could you please share your concerns about this. |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
Does it fix #13848, repository for my issue is at https://github.com/HimanshuBansal112/NetMauiIssue for testing? |
You can try a nightly build to figure out if the issue is fixed or not. |
Root cause
The issue occurs because the BindingContext is set to null after navigating to another page. As a result, when returning to the page, the MenuFlyoutItem loses its binding, causing the Command to stop functioning properly.
Description of Issue Fix
The fix involves the parent of the MenuBarItem only set to null when the parent of MenuBarItem is MenuBar instead of page. This prevents the unintended loss of the BindingContext, which can disrupt bindings and commands when navigating back to a page.
Tested the behavior in the following platforms.
The MenuBar control is not supported on iOS and Android, so this fix was not tested on these platforms.
Issues Fixed
Fixes #21675
Output
BeforeFix-MenuBarItem-Windows.mp4
AfterFix-MenuBarItem-Windows.mp4