dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.87k stars 1.69k forks source link

Maui.Controls.Compatibility.StackLayout; add children, chidlren.clear, re-add children -> System.Object.DisposedException #22575

Open HoGo72 opened 1 month ago

HoGo72 commented 1 month ago

Description

On Android -14-Simulator I get "System.ObjectDisposedException" after re-adding a Microsoft.Maui-Compatibility.StackLayout to another Stacklayout. iOS-Simulator: OK Tizen: not testet Mac: not testet Windows: not tested because of "The handler's Maui-Context cannot be null."-Exception during UseMauiCompatibility

Steps to Reproduce

Create Sample-Maui-Project, replace MainPage with this: using CStackLayout=Microsoft.Maui.Controls.Compatibility.StackLayout;

namespace MauiApp1 {

public class MainPage : ContentPage
{
    CStackLayout m_ContentStack= new CStackLayout();
    Label m_LabelA = new Label { Text = "some Text A" };
    Label m_LabelB = new Label { Text = "some Text B" };
    Button m_Button;
    CStackLayout m_MainView;

    public MainPage()
    {
        m_ContentStack.Children.Add(m_LabelA);
        m_ContentStack.Children.Add(m_LabelB);

        m_Button = new Button { Text = "Update", Command = new Command(OnUpdate) };
        m_MainView = new CStackLayout();
        AddContentNow();
        Content = m_MainView;
    }

    void AddContentNow()
    {
        m_MainView.Children.Add(m_ContentStack);
        m_MainView.Children.Add(m_Button);
    }

    void OnUpdate()
    {
        m_MainView.Children.Clear();
        AddContentNow();
    }

}

}

Change to Android-Simulator, start program, press "Update"-Button -> Exception is thrown

Btw: 1) change to "using CStackLayout = Microsoft.Maui.Controls.StackLayout;" repairs the problem. But I migrate from Xamarin, so I have to use Compatibility. 2) In my real- life project I get in similar case Java.Lang.IllegalStateException: 'The specified child already has a parent. You must call removeView() on the child's parent first.' I hope that's the same problem...

Link to public reproduction project repository

https://github.com/HoGo72/MauiCompatibilityStacklayoutCrash.git

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

PureWeen commented 1 month ago

@HoGo72 if you don't use a compatibility StackLayout does the same thing happen?

HoGo72 commented 1 month ago

@HoGo72 if you don't use a compatibility StackLayout does the same thing happen?

No, Microsoft.Maui.Controls.StackLayout works. But I migrate from Xamarin with heavy use of Forms.StackLayout./...AndExpand. So I cannot use this.

PureWeen commented 1 month ago

@HoGo72 you can maybe workaround this by calling disconnecthandler on the child views

(view.Handler.DisconnectHandler) before adding it to the new view.

HoGo72 commented 1 month ago

@HoGo72 you can maybe workaround this by calling disconnecthandler on the child views

(view.Handler.DisconnectHandler) before adding it to the new view. Thx, yes, workaround works. But you'll need also to handle sub-children, even if view.Handler is null.