Skip to content
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

The specified child already has a parent. You must call removeView() on the child's parent first. #6

Open
ntbao17 opened this issue Apr 2, 2024 · 11 comments

Comments

@ntbao17
Copy link

ntbao17 commented Apr 2, 2024

I'm using the RGPopup Maui package for the PopUp page in my MAUI project.


1
2
3

I got error
Exception: Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
But when I always create a new instance there is no error
4

@microspaze
Copy link
Owner

Let me have a look.

microspaze added a commit that referenced this issue Apr 2, 2024
microspaze added a commit that referenced this issue Apr 2, 2024
… must call removeView() on the child's parent first. #6
@microspaze
Copy link
Owner

Fixed in latest version 1.0.6 https://www.nuget.org/packages/RGPopup.Maui/1.0.6

Thanks for reporting!

@ntbao17
Copy link
Author

ntbao17 commented Apr 2, 2024

There are still errors, please review and help!
The first popup show was successful, the second time it got this error

Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.Border is already a child of ClubSync.Views.Popup.BasePopup. Remove Microsoft.Maui.Controls.Border from ClubSync.Views.Popup.BasePopup before adding to Microsoft.Maui.Controls.ContentView.
[0:] Exception: Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 20830
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 75
at Android.Views.ViewGroup.AddView(View child) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Views.ViewGroup.cs:line 2148
at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 42
at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 47
at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172 at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47 at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82 at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 26
at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921 at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863 at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031 at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96 at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127 at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 41 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(IBorderHandler handler) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:line 45
at Microsoft.Maui.Handlers.BorderHandler.MapContent(IBorderHandler handler, IBorderView border) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.cs:line 163
at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IBorderHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172 at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47 at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82 at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:line 28
at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921 at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863 at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031 at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96 at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127 at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 41 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(IBorderHandler handler) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:line 45
at Microsoft.Maui.Handlers.BorderHandler.MapContent(IBorderHandler handler, IBorderView border) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.cs:line 163
at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IBorderHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172 at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47 at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82 at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:line 28
at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56 at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921 at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863 at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031 at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96 at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127 at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 42 at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 47 at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].b__0(IElementHandler h, IElement v) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 172
at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 47
at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 82
at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79
at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53 at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 26 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2031
at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 42
at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 47
at Microsoft.Maui.PropertyMapper2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 172 at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47 at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82 at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79 at Microsoft.Maui.Handlers.ViewHandler2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 26
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
at Microsoft.Maui.Controls.VisualElement.set_Handler(IViewHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1709
at RGPopup.Maui.Extensions.HandlerExtension.GetOrCreateHandler[PopupPageHandlerDroid](VisualElement bindable)
at RGPopup.Maui.Droid.Impl.PopupPlatformDroid.AddAsync(PopupPage page)
at RGPopup.Maui.Services.PopupNavigationImpl.AddAsync(PopupPage page)
at RGPopup.Maui.Services.PopupNavigationImpl.<>c__DisplayClass20_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at RGPopup.Maui.Services.PopupNavigationImpl.<>c__DisplayClass28_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---

@microspaze
Copy link
Owner

microspaze commented Apr 2, 2024

Can you provide the BasePopup's code and xaml? I had added some test demo code to reproduce this issue and fixed it. Maybe there is some more issues which are not been fixed.

Here is the test demo case I added: 516d3f0

@ntbao17
Copy link
Author

ntbao17 commented Apr 2, 2024

Please review and help!
The first popup show was successful, then closed, then pressed the show button the second time and got this error
code.zip

@microspaze
Copy link
Owner

I still can not find the root cause, but I found a walk-around.

public class UIHelper
    {
        private static BasePopup? _popup = null;

        public async static Task ShowPopup(ContentView contentView, string title, bool closeTouchOutside = true, bool showTitle = true, bool usePadding = true, Action completed = null, Action actionWhenClickOutSide = null, bool useBorder = true)
        {
            Debug.WriteLine($"SHOW POPUP: {contentView.GetType().Name}");

            if (_popup == null)
            {
                _popup = new Views.Popup.BasePopup(contentView, title, closeTouchOutside, showTitle, actionTouchOutSize: actionWhenClickOutSide, useBorder: useBorder);
            }
            else
            {
                _popup.UpdateContent(contentView, title, closeTouchOutside, showTitle, actionTouchOutSize: actionWhenClickOutSide, useBorder: useBorder);
            }
            if (!usePadding)
                _popup.RemovePadding();
            await _popup.ShowPopup();//Navigation.PopPopupAsync();
            GLOBAL.GlobalProperties.CurrentPopup = _popup;
            completed?.Invoke();
        }

        public static bool CloseAllPopup()
        {
            if (RGPopup.Maui.Services.PopupNavigation.Instance.PopupStack.Count > 0)
            {
                RGPopup.Maui.Services.PopupNavigation.Instance.PopAllAsync();
                return true;
            }
            return false;
        }
    }

In UIHelper class, use a static BasePopup instance to popup content view. You can try it. I will take a deeper look for the root casue later.

@ntbao17
Copy link
Author

ntbao17 commented Apr 3, 2024

Thank you, I couldn't find the UpdateContent function.

@microspaze
Copy link
Owner

microspaze commented Apr 3, 2024

Thank you, I couldn't find the UpdateContent function.

UpdateContent function is just like BasePopup's constructor. Looks like:

public void UpdateContent(ContentView contentView, string title, bool closeTouchOutside = true, bool showTitle = true, Action actionTouchOutSize = null, bool useBorder = true)
        {
            ContentType = contentView.GetType();
            ActionTouchOutSize = actionTouchOutSize;
            viewContent.Content = contentView;
            lb_title.Text = title;
            v_Title.IsVisible = showTitle;
            borderContent.Padding = showTitle ? new Thickness(20) : new Thickness(0);
            borderContent.StrokeThickness = useBorder ? 1.0 : 0.0;
        }

@ntbao17
Copy link
Author

ntbao17 commented Apr 3, 2024

I have solved the problem thank you.

microspaze added a commit that referenced this issue Apr 3, 2024
… a parent. You must call removeView() on the child's parent first. #6
@microspaze
Copy link
Owner

Finally fixed in latest version: https://www.nuget.org/packages/RGPopup.Maui/1.0.7

The root cause is the dynamic-added popup content view. If you add sub elements dynamiclly to a initialized PopupPage, then you should handle the Native view cleanup by yourself. (https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/create?view=net-maui-8.0#native-view-cleanup)

There 3 ways to fix this problem:
1. The first one is to disconnect content view's handler before popup disposed. Just like:

public partial class BasePopupPage : PopupPage
    {
        .....
        protected override void OnDisappearing()
        {
            //Fix error: Java.Lang.IllegalStateException
            //Message=The specified child already has a parent. You must call removeView() on the child's parent first.
            viewContent.Content.Handler?.DisconnectHandler();
            base.OnDisappearing();
        }
    }

2. The second one is to use a static BasePopupPage to hold the popup content view. Just like:

public class PopupHelper
    {
        private static BasePopupPage? _popup = null;

        public static async Task ShowPopup(ContentView content, string title = "", bool staticMode = false)
        {
            if (staticMode)
            {
                _popup ??= new BasePopupPage(content, title);
                _popup.UpdateContent(content, title);
                await _popup.ShowPopup();
            }
            else
            {
                var popup = new BasePopupPage(content, title);
                await popup.ShowPopup();
            }
        }
    }

Use staticMode = true will always popup the _popup instance.

3. The last one is to handle the disconnections by RGPopup.Maui lib. It will auto disconnect all sub elements' handlers when popup page unloads.

private void OnPopupUnloaded(object? sender, EventArgs e)
        {
            if (sender is PopupPage page)
            {
                //Auto disconnect all sub elements' handler.
                //Fix The specified child already has a parent. You must call removeView() on the child's parent first.
                var elements = page.GetVisualTreeDescendants().OfType<IElement>().ToList();
                foreach (var element in elements)
                {
                    element.Handler?.DisconnectHandler();
                }
            }
        }

@ntbao17 You can upgrade the RGPopup.Maui to 1.0.7 to verify the last solution when you are free.

@microspaze
Copy link
Owner

BTW there is a awesome lib I'd like to suggest to use in MAUI development which calls MemoryToolkit.Maui (https://github.com/AdamEssenmacher/MemoryToolkit.Maui)
It can help to prevent some leaks and ensure native resources are cleaned up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants