-
Notifications
You must be signed in to change notification settings - Fork 339
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
Add routing of BindingContext of Popup #1309
Conversation
Does the issue happen only on Apple devices? |
Hi, @VladislavAntonyuk |
the |
Thank you for your review.
The cause is that the content of the Popup is replaced with the content of the newly generated ContentPage.
For this, I added the code below.
But that is wrong. Add the following instead of the above
And I should have changed SetBinding of ContentPage as below.
From the above, I think it will be as follows.
By correcting it as above, it propagates correctly. |
Hi, @VladislavAntonyuk [iOS] iPhone.14.iOS.16.4.2023-07-31.09-03-21.mp4This issue does not occur on Windows and Android. This issue occurs only on iOS. [Android] Android.Emulator.-.pixel_2_-_api_30_5554.2023-07-31.09-07-45.mp4[Windows] 2023-07-31.09-13-58.mp4 |
Below is the reproduction code I created to confirm that there is a problem with the method of BindingContext propagation. https://github.com/cat0363/MauiComm-ReproPopupPicker.git The above partially mimics a portion of the Popup.macios.cs file. Instead of displaying the Popup on the screen, I set the Content of the ContentView imitating the Popup to the [MainPage.xaml]
[Popup.xaml]
The processing when the Show button is pressed is as follows.
If you run it as above, the same phenomenon will be reproduced. Therefore, modify the code as follows. This is equivalent to the solution I posted today.
You can see that the BindingContext is propagated correctly. |
It looks weird why we need it only for macios. Could it be a .NET MAUI issue as they don't set the binding context? |
@VladislavAntonyuk, Thank you for your reply. https://github.com/cat0363/MauiComm-ReproPopupPicker.git Below are the results of running the above code on Android and Windows. [Android] [Windows] If it is made similarly, this problem will occur on Android and Windows. On Android, the LayoutViewGroup obtained by calling the ToPlatform method of the Popup's Content is set to the Dialog's ContentView. [src\CommunityToolkit.Maui.Core\Views\Popup\MauiPopup.android.cs] : TryCreateContainer
On Windows, the LayoutPanel obtained by calling the View's ToPlatform method is added to the Panel's child elements. [src\CommunityToolkit.Maui\Views\WrapperControl.windows.cs] : Constructor
[src\CommunityToolkit.Maui.Core\Views\Popup\MauiPopup.windows.cs] : CreateControl
At least, Android and Windows are built differently than iOS. Does it mean that the behavior that the SelectedItem of the Picker is initialized when the parent is replaced is not what you intended? I'm sorry if my understanding is wrong. |
I tried to find out why SelectedItem is initialized when the parent is replaced. When setting the Popup's Content to the ContentPage's Content, the Popup's Content is removed from the Popup. Then add that Content to the ContentPage.
BindingContext is propagated when the following is executed.
When the above is executed, BindingContext is propagated from Popup to Content of ContentPage. The above is the result of analyzing the source code of .NET MAUI. [src\Controls\src\Core\Element\Element.cs] OnChildAdded So what should be, Only the code below is sufficient to solve this issue.
I didn't need the code below.
By setting the Binding of the Content of the Popup in advance, the BindingContext of the Content will not be iPhone.14.iOS.16.4.2023-08-01.17-10-07.mp4Check out the implementation below to see why. [src\Controls\src\Core\BindableObject.cs] SetInheritedBindingContext |
@cat0363 thanks for all explanation and the fix... I think this is a fine change to do. But looks like a bug on Maui side of the BindingContext, so let's say that you have a <ContentPage>
<Label Text="{Binding Title}" />
</ContentPage> Then on your code-behind you have this: class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
} So, we have the With that, the behavior of the popup and picker should be the same, don't you agree? Because of that, I believe there's a bug or the |
@pictos, Thank you for the easy-to-understand explanation. I agree with your explanation. Certainly it's not that the BindingContext isn't When the parent is replaced, the ItemsSource is initialized to null, at which time I've found that the initialization that occurs when the parent is swapped causes In the Discussions below, I am asking a similar question to the .NET MAUI side. If it's a bug, it seems that BindingContext initialization and Picker behavior are deeply related. . . One workaround I can think of is something like this:
Instead of setting the BindingContext in the constructor of the class that inherits I will leave the closing of this PR to you. If there is a workaround that can be considered at this time, |
This PR solves the problem that the BindingContext of Popup is not routed to ContentPage.
Description of Change
I added the following code to pass the Popup's BindingContext to the ContentPage.
[src\CommunityToolkit.Maui\HandlerImplementation\Popup\Popup.macios.cs]
This will take over the BindingContext and not initialize it with null.
Linked Issues
PR Checklist
approved
(bug) orChampioned
(feature/proposal)main
at time of PRAdditional information
I simplified the program of Issue #1166 and verified it.
Below are the verification results.
iPhone.14.iOS.16.4.2023-07-28.17-30-09.mp4