Avalonia
Avalonia copied to clipboard
the control already has a visual parent Error
I tried to add a lot of button to a ListBox But I keep getting this error : the control already has a visual parent
The error is trigered from here: public static void Main(string[] args) => BuildAvaloniaApp() .StartWithClassicDesktopLifetime(args);
the StackTrace :
at Avalonia.Visual.ValidateVisualChild(IVisual c) at Avalonia.Collections.AvaloniaList
1.Add(T item)
at Avalonia.Controls.Presenters.ContentPresenter.UpdateChild()
at Avalonia.Controls.Presenters.ContentPresenter.ContentChanged(AvaloniaPropertyChangedEventArgs e)
at Avalonia.Controls.Presenters.ContentPresenter.<>c.<.cctor>b__14_0(ContentPresenter x, AvaloniaPropertyChangedEventArgs e)
at System.Reactive.Subjects.Subject1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\Subject.cs:line 148 at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.AvaloniaObject.PriorityValueChanged(AvaloniaProperty property, Int32 priority, Object oldValue, Object newValue) at Avalonia.PriorityValue.SetAndNotify(ValueTuple
2& backing, ValueTuple2 update) at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple
2& backing, ValueTuple2 value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler1 setAndNotifyHandler, TValue& backing, TValue value) at Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority) at Avalonia.Reactive.SingleSubscriberObservableBase
1.PublishNext(T value)
at Avalonia.Data.TemplateBinding.PublishValue()
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.AvaloniaObject.PriorityValueChanged(AvaloniaProperty property, Int32 priority, Object oldValue, Object newValue)
at Avalonia.PriorityValue.SetAndNotify(ValueTuple2& backing, ValueTuple
2 update)
at Avalonia.PriorityValue.Avalonia.Utilities.ISetAndNotifyHandler<(System.Object,System.Int32)>.HandleSetAndNotify(AvaloniaProperty property, ValueTuple2& backing, ValueTuple
2 value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotifyCallback[TValue](AvaloniaProperty property, ISetAndNotifyHandler
1 setAndNotifyHandler, TValue& backing, TValue value)
at Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority)
at Avalonia.ValueStore.AddValue(AvaloniaProperty property, Object value, Int32 priority)
at Avalonia.AvaloniaObject.SetStyledValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.Controls.Generators.ItemContainerGenerator1.TryRecycle(Int32 oldIndex, Int32 newIndex, Object item) at Avalonia.Controls.Presenters.ItemVirtualizerSimple.RecycleContainersForMove(Int32 delta) at Avalonia.Controls.Presenters.ItemVirtualizerSimple.set_OffsetValue(Double value) at Avalonia.Controls.Presenters.ItemVirtualizer.set_Offset(Vector value) at Avalonia.Controls.Presenters.ItemsPresenter.Avalonia.Controls.Primitives.IScrollable.set_Offset(Vector value) at Avalonia.Controls.Presenters.ScrollContentPresenter.<>c__DisplayClass38_0.<UpdateScrollableSubscription>b__3(Vector x) at System.Reactive.AnonymousSafeObserver
1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\AnonymousSafeObserver.cs:line 44
at System.Reactive.Sink1.ForwardOnNext(TTarget value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Internal\Sink.cs:line 50 at System.Reactive.Linq.ObservableImpl.Skip
1.Count._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\Skip.cs:line 59
at Avalonia.Reactive.LightweightObservableBase1.PublishNext(T value) at Avalonia.Reactive.AvaloniaPropertyObservable
1.PropertyChanged(Object sender, AvaloniaPropertyChangedEventArgs e)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 property, T& field, T value) at Avalonia.Controls.Presenters.ScrollContentPresenter.set_Offset(Vector value) at Avalonia.Controls.Presenters.ScrollContentPresenter.<>c.<.cctor>b__12_8(ScrollContentPresenter o, Vector v) at Avalonia.DirectProperty
2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0()
at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value)
at Avalonia.AvaloniaObject.DirectBindingSubscription.OnNext(Object value)
at Avalonia.Reactive.SingleSubscriberObservableBase1.PublishNext(T value) at Avalonia.Data.TemplateBinding.PublishValue() at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.Utilities.DeferredSetter
1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty
1 property, T& field, T value)
at Avalonia.Controls.ScrollViewer.set_Offset(Vector value)
at Avalonia.Controls.ScrollViewer.set_VerticalScrollBarValue(Double value)
at Avalonia.Controls.ScrollViewer.<>c.<.cctor>b__16_16(ScrollViewer o, Double v)
at Avalonia.DirectProperty2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value) at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0() at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value) at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority) at Avalonia.Data.TemplateBinding.System.IObserver<System.Object>.OnNext(Object value) at Avalonia.Reactive.LightweightObservableBase
1.PublishNext(T value)
at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
at Avalonia.Utilities.DeferredSetter1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.Utilities.DeferredSetter1.SetAndNotify(AvaloniaObject source, AvaloniaProperty
1 property, TSetRecord& backing, TSetRecord value)
at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty1 property, T& field, T value) at Avalonia.Controls.Primitives.RangeBase.set_Value(Double value) at Avalonia.Controls.Primitives.RangeBase.<>c.<.cctor>b__28_5(RangeBase o, Double v) at Avalonia.DirectProperty
2.Avalonia.IDirectPropertyAccessor.SetValue(IAvaloniaObject instance, Object value)
at Avalonia.AvaloniaObject.<>c__DisplayClass54_0.<SetDirectValue>g__Set|0()
at Avalonia.AvaloniaObject.SetDirectValue(AvaloniaProperty property, Object value)
at Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
at Avalonia.Data.TemplateBinding.System.IObserver<System.Object>.OnNext(Object value)
at Avalonia.Reactive.LightweightObservableBase1.PublishNext(T value) at Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority) at Avalonia.Utilities.DeferredSetter
1.SetAndRaisePropertyChanged(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.Utilities.DeferredSetter
1.SetAndNotify(AvaloniaObject source, AvaloniaProperty1 property, TSetRecord& backing, TSetRecord value) at Avalonia.AvaloniaObject.SetAndRaise[T](AvaloniaProperty
1 property, T& field, T value)
at Avalonia.Controls.Primitives.Track.set_Value(Double value)
at Avalonia.Controls.Primitives.Track.ThumbDragged(Object sender, VectorEventArgs e)
at Avalonia.Interactivity.Interactive.<AddHandler>g__InvokeAdapter|7_0[TEventArgs](Delegate baseHandler, Object sender, RoutedEventArgs args)
at Avalonia.Interactivity.Interactive.RaiseEventImpl(RoutedEventArgs e)
at Avalonia.Interactivity.Interactive.HierarchyTraverser2.Traverse(IInteractive target) at Avalonia.Interactivity.Interactive.BubbleEvent(RoutedEventArgs e) at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) at Avalonia.Controls.Primitives.Thumb.OnPointerMoved(PointerEventArgs e) at System.Reactive.AnonymousObserver
1.OnNextCore(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\AnonymousObserver.cs:line 67
at System.Reactive.ObserverBase1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\ObserverBase.cs:line 36 at System.Reactive.Subjects.Subject
1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\Subject.cs:line 150
at Avalonia.Interactivity.Interactive.RaiseEventImpl(RoutedEventArgs e)
at Avalonia.Interactivity.Interactive.HierarchyTraverser2.Traverse(IInteractive target) at Avalonia.Interactivity.Interactive.BubbleEvent(RoutedEventArgs e) at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) at Avalonia.Input.MouseDevice.MouseMove(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties properties, KeyModifiers inputModifiers) at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e) at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e) at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg) at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) at TestPerf.Program.Main(String[] args) in C:\Users\axel\source\repos\TestPerf\TestPerf\Program.cs:line 13
My Code : XAML : ` <ListBox Items="{Binding MyItems1}" Width="800" x:Name="ControlBlc">
</ListBox>`
C# : `public MainWindow() { InitializeComponent(); #if DEBUG this.AttachDevTools(); #endif ListBox c = this.FindControl<ListBox>("ControlBlc"); Bitmap r = new Bitmap("aa.png"); ObservableCollection<Button> gt = new ObservableCollection< Button >(); for (int g = 0; g < 10000; g++) { Button t = new Button(); t.Background = new ImageBrush(r); t.Width = 50; t.Height = 50;
gt.Add(t);
}
g = new ContextItem { MyItems1 = gt };
c.DataContext = g;
}
public class ContextItem
{
private ObservableCollection<Button> MyItems;
public ObservableCollection<Button> MyItems1
{
get
{
return MyItems;
}
set
{
MyItems = value;
}
}
}
public ContextItem g;`
I am experiencing this issue also. I'm on Windows 10, v0.10.0-preview1 (although I also tried 0.9.999-cibuild0008974 and it still happens). In my case, all of the items were added to the ListBox in XAML.
An unhandled exception of type 'System.InvalidOperationException' occurred in Avalonia.Visuals.dll
The control already has a visual parent.
The problem seems to be with ListBox scrolling, possibly item virtualization. For me:
- 14 ListBoxItem(s) added in XAML fit exactly in the ListBox and do not involve any scrolling. Works fine.
- Adding a 15th ListBoxItem triggers the display of the vertical scrollbar, and items can now be scrolled through vertically. Everything still works, even if you scroll up and down repeatedly.
- Adding a 16th ListBoxItem causes the last entry to be displayed horizontally indented compared to the others, and scrolling down to the bottom and then up will 100% of the time crash the application with the before-mentioned error.
- Setting the ListBox VirtualizationMode to "None" no longer crashes on scroll, even if I continue adding more items.
I am experiencing this issue when resizing my Window.
- Change Window (800x450) to full screen size (not F11, just maximize)
- Change it back to normal size
- Crash (System.InvalidOperationException: The control already has a visual parent.)
I was also able to reproduce this very simply by just this code (Avalonia 0.10.0, .NET 5):
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Width="600" Height="450"
x:Class="AvaloniaApp.MainWindow"
Title="AvaloniaApp">
<ListBox>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
<ListBoxItem>Item4</ListBoxItem>
<ListBoxItem>Item5</ListBoxItem>
<ListBoxItem>Item6</ListBoxItem>
<ListBoxItem>Item7</ListBoxItem>
<ListBoxItem>Item8</ListBoxItem>
<ListBoxItem>Item9</ListBoxItem>
<ListBoxItem>Item10</ListBoxItem>
<ListBoxItem>Item11</ListBoxItem>
<ListBoxItem>Item12</ListBoxItem>
<ListBoxItem>Item13</ListBoxItem>
<ListBoxItem>Item14</ListBoxItem>
<ListBoxItem>Item15</ListBoxItem>
</ListBox>
</Window>
Just opening the application and scrolling the listbox throws this exception:
I literally started a new Avalonia project, added an example listbox (above), and hit an exception by just scrolling the listbox. Seems like a big deal.
Note: I found that the issue can be worked around by binding the Items
property of the listbox rather than setting it in xaml. Then the exception does not seem to appear
Virtualization can't properly work with hardcoded control items instead of MVVM-like approach with item sources and templates. Setting "VirtualizationMode="None"" fixes the problem. AFAIK it also is not reproduced with ItemsRepeater, so this PR should remove the problem https://github.com/AvaloniaUI/Avalonia/pull/4779. I probably wouldn't expect fixing old virtualization in that case.
@maxkatz6 I see your solution response, I know you suggested to set "VirtualizationMode="None"", however, I don't know where to put that. I attempted to put it in several places, but no element seemed to recognize the property. For reference, I'm getting the error on a MenuItem click.
**Edit** I found another related issue. #5441 One to which you've also replied to. But that one was using a combobox. So I tried switching to using a combobox instead of a menuitem. However, despite literally copy/pasting the solution that the OP of that issue said worked for them. It did not work for me.
@General-Ridley same exception can't possibly happen if virtualization is disabled. Do you have different stacktrace? Or maybe steps to reproduce are different?
To disable virtualization you need to change VirtualizationMode property on ItemsPresenter. ComboBox has a property for convenience that also changes virtualization mode of the item presenter inside.
I haven't tested, but something like this should change this mode for the menu item as well:
<Style Selector="MenuItem /template/ ItemsPresenter">
<Setter Property="VirtualizationMode" Value="None" />
</Style>
@maxkatz6 thanks for the reply.
I attempted using the styles as per your recommendation. However, the error persisted for me.
The gist of it is, I have the style you gave in the Window.Styles element. And later in the axaml I have the Menu and a few nested MenuItem elements. I'm using the menu to allow the user to toggle the app's theme from Light to Dark. It will work on the first go. But if the user tries to toggle back, the app crashes on the MenuItem that is the parent of the MenuItems that toggle the theme.
I can show you my code setup. And I have the stacktrace; it is sliiiiightly different.
But I'm not sure if it's appropriate to continue using this issue to discuss it here, or open a new issue. Which would you prefer?