microspaze / RGPopup.Maui

Popup controls for .NET MAUI migrated from Rg.Plugins.Popup for Xamarin with some fixes.
MIT License
39 stars 8 forks source link

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

Open ntbao17 opened 6 months ago

ntbao17 commented 6 months ago

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 commented 6 months ago

Let me have a look.

microspaze commented 6 months ago

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

Thanks for reporting!

ntbao17 commented 6 months ago

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.<>cDisplayClass5_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.PropertyMapper`2.<>cDisplayClass5_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]].b0(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.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.<>cDisplayClass20_0.<b0>d.MoveNext() --- End of stack trace from previous location --- at RGPopup.Maui.Services.PopupNavigationImpl.<>cDisplayClass28_0.<b__0>d.MoveNext() --- End of stack trace from previous location ---

microspaze commented 6 months ago

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: https://github.com/microspaze/RGPopup.Maui/commit/516d3f0e6fafbf85a7953dedf814fb52c3c720fb

ntbao17 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

microspaze commented 6 months ago

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 commented 6 months ago

I have solved the problem thank you.

microspaze commented 6 months ago

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 commented 6 months ago

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.