Avalonia
Avalonia copied to clipboard
UserControl loses its visual children when reused by TransitioningContentControl
Describe the bug
UserControl
loses its visual children when reused by TransitioningContentControl
. The issue doesn't happen anymore if a ControlControl
is used instead but then I lose the transition. I attached a minimal reproduction below.
To Reproduce Steps to reproduce the behavior:
- Run the reproduction project attached below.
- Click on 'Switch view', observe that the text changed to "Hello from user control!".
- Click on 'Switch view', it should be back to "Hello world! 1" again.
- Click on 'Switch view', we expect "Hello from user control!" again but it's gone.
Expected behavior It should show "Hello from user control!" again.
Desktop (please complete the following information):
- OS: Windows (tho I assume the issue is platform-agnostic)
- Version 11.0.0
Additional context
The TestUserControl
contains code that prints out when it is attached/detached to a visual tree and when its VisualChildren changes as follows:
using System.Diagnostics;
using Avalonia.Controls;
namespace TestAvalonia.Views;
public partial class TestUserControl : UserControl
{
public TestUserControl()
{
InitializeComponent();
VisualChildren.CollectionChanged += (sender, args) =>
{
Debug.WriteLine($"VisualChildren.CollectionChanged: {args.Action}");
};
AttachedToVisualTree += (sender, args) =>
{
Debug.WriteLine($"AttachedToVisualTree");
};
DetachedFromVisualTree += (sender, args) =>
{
Debug.WriteLine($"DetachedFromVisualTree");
};
}
}
During reproduction, the following will be observed:
// click - Step 2 of repro steps
AttachedToVisualTree
VisualChildren.CollectionChanged: Add
// click - Step 3 of repro steps
DetachedFromVisualTree
// click - Step 4 of repro steps
AttachedToVisualTree
VisualChildren.CollectionChanged: Remove
// click
DetachedFromVisualTree
// click
AttachedToVisualTree
// click
DetachedFromVisualTree
// click
AttachedToVisualTree
I added annotation of when a click happens and formatted it a bit
As can be seen here, the visual children is somehow cleared when the user control is supposed to show again, and the children is never added back.
The repro project also contains some commented code in MainWindow.axaml
and MainWindow.axaml.cs
that changes the behaviour of the issue.
MainWindow.axaml
<Grid RowDefinitions="*,Auto">
<!-- <TransitioningContentControl Name="PART_Panel" Grid.Row="0"/> -->
<ContentControl Name="PART_Panel" Grid.Row="0"/>
<Button Content="Switch view" Click="HandleButtonClick" Grid.Row="1"/>
</Grid>
Using a ContentControl
instead of TransitioningContentControl
makes it so the visual children is never cleared, thus "fixing" the issue, but at the expense of transitions.
MainWindow.axaml.cs
private Control[] _controls = new Control[]
{
new TextBlock()
{
Text = "Hello World! 1"
},
new TestUserControl(),
new ContentControl()
{
Content = new TextBlock()
{
Text = "Hello World! 2"
}
}
};
Making the TransitioningContentControl
encounter another content control makes this weird behavior of it showing and not showing.
// click - Hello from user control!
AttachedToVisualTree
VisualChildren.CollectionChanged: Add
// click - Hello World! 2
DetachedFromVisualTree
// click - Hello World! 1
// click - Hello from user control! - invisible
AttachedToVisualTree
VisualChildren.CollectionChanged: Remove
// click - Hello World! 2
DetachedFromVisualTree
// click - Hello World! 1
// click - Hello from user control!
AttachedToVisualTree
VisualChildren.CollectionChanged: Add
... loops