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

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

Open
HoGo72 opened this issue May 22, 2024 · 5 comments
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Milestone

Comments

@HoGo72
Copy link

HoGo72 commented May 22, 2024

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

@HoGo72 HoGo72 added the t/bug Something isn't working label May 22, 2024
Copy link
Contributor

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
Copy link
Member

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

@PureWeen PureWeen added the s/needs-info Issue needs more info from the author label May 22, 2024
@HoGo72
Copy link
Author

HoGo72 commented May 23, 2024

@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.

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels May 23, 2024
@PureWeen
Copy link
Member

PureWeen commented May 23, 2024

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

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

@PureWeen PureWeen added area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert labels May 23, 2024
@PureWeen PureWeen added this to the Backlog milestone May 23, 2024
@HoGo72
Copy link
Author

HoGo72 commented May 28, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants