Skip to content

brminnick/LayoutChildAddedRepro

Repository files navigation

LayoutChildAddedRepro

A .NET MAUI 6.0.100 reproduction sample for Layout.ChildAdded.

Bug Description

The Layout.ChildAdded event does not fire when an IView is added to a Layout like a Grid or a StackLayout.

protected override void OnAppearing()
{
  base.OnAppearing();

  var stackLayout = new StackLayout();
  stackLayout.ChildAdded += HandleChildAdded
    
  stackLayout.Children.Add(new BoxView()); // `ChildAdded` does not fire
}

// This Event Handler Does Not Execute because `ChildAdded` does not fire
async void HandleChildAdded(object sender, ElementEventArgs e) =>
 await DisplayAlert("Child Added", $"Added a {e.Element.GetType().FullName}", "OK");

Reproduction Steps

  1. Install .NET MAUI
  2. Download/Clone this repo: https://github.com/brminnick/LayoutChildAddedRepro
  3. In MainPage.xaml, confirm ChildAdded is subscribed to HandleChildAdded
  4. In MainPage.xaml.cs, confirm OnAppearing triggers the following DisplayAlert:
    await DisplayAlert("OnAppearing Fired", "Click OK to add a Green Box View", "OK");
  5. In MainPage.OnAppearing, confirm a green BoxView is added to the `Grid:
    var greenBox = new BoxView { BackgroundColor = Colors.Green };
    GridLayout.SetRow(greenBox, 1);
    GridLayout.SetColumn(greenBox, 0);
    
    MainGrid.Add(greenBox);
  6. Confirm HandleChildAdded triggers DisplayAlert:
    async void HandleChildAdded(object sender, ElementEventArgs e)
    {
      await DisplayAlert("Child Added", $"Added a {e.Element.GetType().FullName}", "OK");
    }
  7. In the terminal, navigate to the downloaded LayoutChildAddedRepro repo
  8. In the terminal, run LayoutChildAddedRepro.sln by entering the following command for your specific simulator/device
    • iOS: dotnet build -t:run -f:net6.0-ios
    • Android: dotnet build -t:run -f:net6.0-android
    • MacCatalyst: dotnet build -t:run -f:net6.0-maccatalyst
  9. In the simulator/device, confirm the DisplayAlert appears

image

  1. In the simulator, click OK
  2. In the simulator, confirm a Green Box is added to MainPage

image

  1. In the simulator, confirm the second DisplayAlert does not appear
  • If ChildAdded fires, a second DisplayAlert should appear, but ChildAdded does not fire

About

A .NET MAUI reproduction sample for `Layout.ChildAdded`

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages