A .NET MAUI 6.0.100 reproduction sample for Layout.ChildAdded
.
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");
- Install .NET MAUI
- Download/Clone this repo: https://github.com/brminnick/LayoutChildAddedRepro
- In
MainPage.xaml
, confirmChildAdded
is subscribed toHandleChildAdded
- In
MainPage.xaml.cs
, confirmOnAppearing
triggers the followingDisplayAlert
:await DisplayAlert("OnAppearing Fired", "Click OK to add a Green Box View", "OK");
- In
MainPage.OnAppearing
, confirm a greenBoxView
is added to the `Grid:var greenBox = new BoxView { BackgroundColor = Colors.Green }; GridLayout.SetRow(greenBox, 1); GridLayout.SetColumn(greenBox, 0); MainGrid.Add(greenBox);
- Confirm
HandleChildAdded
triggersDisplayAlert
:async void HandleChildAdded(object sender, ElementEventArgs e) { await DisplayAlert("Child Added", $"Added a {e.Element.GetType().FullName}", "OK"); }
- In the terminal, navigate to the downloaded
LayoutChildAddedRepro
repo - 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
- iOS:
- In the simulator/device, confirm the
DisplayAlert
appears
- In the simulator, click OK
- In the simulator, confirm a Green Box is added to
MainPage
- In the simulator, confirm the second
DisplayAlert
does not appear
- If
ChildAdded
fires, a secondDisplayAlert
should appear, butChildAdded
does not fire