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 vp is null vp is set Core2D.Avalonia.Presenters.CachedContentPresenter vp is set Core2D.Avalonia.Presenters.CachedContentPresenter vp is set Core2D.Avalonia.Presenters.CachedContentPresenter vp is null Exception thrown: 'System.InvalidOperationException' in Avalonia.Interactivity.dll Core2D.Avalonia.Skia.vshost.exe Error: 0 : The control already has a visual parent. w Avalonia.Visual.ValidateVisualChild(IVisual c) w Avalonia.Collections.AvaloniaList`1.Add(T item) w Avalonia.Controls.Presenters.ContentPresenter.UpdateChild() w Avalonia.Controls.Presenters.ContentPresenter.ApplyTemplate() w Avalonia.Controls.Mixins.ContentControlMixin.<>c__DisplayClass1_0`1.<Attach>b__0(Object s, RoutedEventArgs ev) --- Koniec śladu stosu z poprzedniej lokalizacji, w której wystąpił wyjątek --- w Avalonia.Interactivity.RoutedEvent.<>c__DisplayClass25_0.<AddClassHandler>b__0(Tuple`2 args) w System.Reactive.AnonymousObserver`1.OnNextCore(T value) w System.Reactive.ObserverBase`1.OnNext(T value) w System.Reactive.Observer`1.OnNext(T value) w Avalonia.Interactivity.Interactive.RaiseEventImpl(RoutedEventArgs e) w Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) w Avalonia.Controls.Primitives.TemplatedControl.OnTemplateApplied(TemplateAppliedEventArgs e) w Avalonia.Controls.Primitives.TemplatedControl.ApplyTemplate() w Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) w Avalonia.Layout.Layoutable.Measure(Size availableSize) w Avalonia.Controls.Presenters.ContentPresenter.MeasureOverride(Size availableSize) w Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) w Avalonia.Layout.Layoutable.Measure(Size availableSize) w Avalonia.Layout.LayoutManager.Measure(ILayoutable control) w Avalonia.Layout.LayoutManager.ExecuteMeasurePass() w Avalonia.Layout.LayoutManager.ExecuteLayoutPass() w Avalonia.Threading.JobRunner.RunJobs() w Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) w Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg) w Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) w Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) w Avalonia.Application.Run(ICloseable closable) w Core2D.Avalonia.App.Start(IFileSystem fileIO, ILog log, ImmutableArray`1 writers) w C:\DOWNLOADS\GitHub-Core2D\Core2D\src\Core2D.Avalonia\App.xaml.cs:wiersz 181

Code:

    public class CachedContentPresenter : ContentPresenter
        private static IDictionary<Type, Func<Control>> _factory = new Dictionary<Type, Func<Control>>();
        private readonly IDictionary<Type, Control> _cache = new Dictionary<Type, Control>();
        public static void Register(Type type, Func<Control> create) => _factory[type] = create;
        public CachedContentPresenter()
            this.GetObservable(DataContextProperty).Subscribe((value) => SetContent(value));
        public Control GetControl(Type type)
            Control control;
            _cache.TryGetValue(type, out control);
            if (control == null)
                Func<Control> createInstance;
                _factory.TryGetValue(type, out createInstance);
                control = createInstance?.Invoke();
                if (control != null)
                    _cache[type] = control;
                    throw new Exception($"Can not find factory method for type: {type}");
            return control;
        public void SetContent(object value)
            Control control = null;
            if (value != null)
                control = GetControl(value.GetType());
            if (control != null)
                var vp = control.GetVisualParent();
                if (vp != null)
                    System.Diagnostics.Debug.WriteLine($"vp is set {vp.GetType()}");
                    System.Diagnostics.Debug.WriteLine("vp is null");
                this.Content = control;
            catch (Exception ex)
                System.Diagnostics.Debug.WriteLine(ex.Message);

Xaml:

<presenters:CachedContentPresenter DataContext="{Binding Project.CurrentContainer.CurrentShape}"/>

@grokys I have made a test with following Xaml being cached:

    <StackPanel>
        <TextBlock Text="Hello from Path" Classes="default"></TextBlock>
        <ListBox>
            <ListBoxItem>Path</ListBoxItem>
        </ListBox>
        <TabControl Classes="default">
            <TabItem Header="Path" Classes="property">
                <TextBlock Text="Hello from TabItem Path" Classes="default"></TextBlock>
            </TabItem>
        </TabControl>
    </StackPanel>

and after few times that everything got rendered only first TextBlock is rendered again.

Video: https://files.gitter.im/AvaloniaUI/Avalonia/yh1X/2016-07-11_22-17-14.mp4

Looks like the issue may be connected to virtualization.

Changed ListBox panel to StackPanel and the ListBox control is not rendered after few times, but ListBoxItem is rendered always!

    <StackPanel Background="Yellow">
        <TextBlock Text="Hello from Path" Classes="default"></TextBlock>
        <Rectangle Width="100" Height="50" Fill="Orange"/>
        <ListBox Name="listBox" Background="Red" VirtualizationMode="Simple">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem>Path</ListBoxItem>
        </ListBox>
    </StackPanel>

If I change VirtualizationMode to none I get after few times this exception:

Exception thrown: 'System.NullReferenceException' in Avalonia.Controls.dll
Core2D.Avalonia.Skia.vshost.exe Error: 0 : Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
   w Avalonia.Controls.Presenters.ItemVirtualizer.Dispose()
   w Avalonia.Controls.Presenters.ItemsPresenter.VirtualizationModeChanged(AvaloniaPropertyChangedEventArgs e)
   w Avalonia.AvaloniaObjectExtensions.SubscribeAdapter[TTarget](AvaloniaPropertyChangedEventArgs e, Func`2 handler)
   w Avalonia.AvaloniaObjectExtensions.<>c__DisplayClass8_0`1.<AddClassHandler>b__0(AvaloniaPropertyChangedEventArgs e)
   w System.Reactive.AnonymousObserver`1.OnNextCore(T value)
   w System.Reactive.ObserverBase`1.OnNext(T value)
   w System.Reactive.Subjects.Subject`1.OnNext(T value)
   w Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
   w Avalonia.AvaloniaObject.Avalonia.IPriorityValueOwner.Changed(PriorityValue sender, Object oldValue, Object newValue)
   w Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority)
   w Avalonia.PriorityValue.LevelValueChanged(PriorityLevel level)
   w Avalonia.PriorityLevel.ActivateFirstBinding()
   w Avalonia.PriorityLevel.Changed(PriorityBindingEntry entry)
   w Avalonia.PriorityBindingEntry.ValueChanged(Object value)
   w System.Reactive.AnonymousSafeObserver`1.OnNext(T value)
   w System.Reactive.Linq.ObservableImpl.Select`2._.OnNext(TSource value)
   w System.Reactive.AutoDetachObserver`1.OnNextCore(T value)
   w System.Reactive.ObserverBase`1.OnNext(T value)
   w System.Reactive.SafeObserver`1.OnNext(TSource value)
   w System.Reactive.Linq.ObservableImpl.TakeUntil`2._.T.OnNext(TSource value)
   w System.Reactive.Subjects.Subject`1.OnNext(T value)
   w Avalonia.Markup.Data.ExpressionNode.set_CurrentValue(WeakReference value)
   w Avalonia.Markup.Data.ExpressionNode.set_Target(WeakReference value)
   w Avalonia.Markup.Data.ExpressionObserver.<IncrementCount>b__27_0(Unit x)
   w System.Reactive.AnonymousSafeObserver`1.OnNext(T value)
   w System.Reactive.Linq.ObservableImpl.Select`2._.OnNext(TSource value)
   w System.Reactive.Linq.ObservableImpl.Skip`1._.OnNext(TSource value)
   w System.Reactive.Linq.ObservableImpl.Cast`2._.OnNext(TSource value)
   w System.Reactive.AutoDetachObserver`1.OnNextCore(T value)
   w System.Reactive.ObserverBase`1.OnNext(T value)
   w Avalonia.AvaloniaObjectExtensions.<>c__DisplayClass0_1.<GetObservable>b__1(Object s, AvaloniaPropertyChangedEventArgs e)
   w System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   w Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
   w Avalonia.AvaloniaObject.Avalonia.IPriorityValueOwner.Changed(PriorityValue sender, Object oldValue, Object newValue)
   w Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority)
   w Avalonia.PriorityValue.LevelValueChanged(PriorityLevel level)
   w Avalonia.PriorityLevel.set_DirectValue(Object value)
   w Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
   w Avalonia.AvaloniaObject.SetValue[T](AvaloniaProperty`1 property, T value, BindingPriority priority)
   w Avalonia.Controls.Mixins.ContentControlMixin.<>c__DisplayClass1_0`1.<Attach>b__3(AvaloniaPropertyChangedEventArgs e)
   w System.Reactive.AnonymousObserver`1.OnNextCore(T value)
   w System.Reactive.ObserverBase`1.OnNext(T value)
   w System.Reactive.Observer`1.OnNext(T value)
   w System.Reactive.Subjects.Subject`1.OnNext(T value)
   w Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
   w Avalonia.AvaloniaObject.ParentPropertyChanged(Object sender, AvaloniaPropertyChangedEventArgs e)
   w System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   w Avalonia.AvaloniaObject.RaisePropertyChanged(AvaloniaProperty property, Object oldValue, Object newValue, BindingPriority priority)
   w Avalonia.AvaloniaObject.Avalonia.IPriorityValueOwner.Changed(PriorityValue sender, Object oldValue, Object newValue)
   w Avalonia.PriorityValue.UpdateValue(Object value, Int32 priority)
   w Avalonia.PriorityValue.LevelValueChanged(PriorityLevel level)
   w Avalonia.PriorityLevel.set_DirectValue(Object value)
   w Avalonia.AvaloniaObject.SetValue(AvaloniaProperty property, Object value, BindingPriority priority)
   w Avalonia.Controls.Control.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Control.Avalonia.LogicalTree.ILogical.NotifyDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Primitives.TemplatedControl.OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Control.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Control.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Control.OnDetachedFromLogicalTreeCore(LogicalTreeAttachmentEventArgs e)
   w Avalonia.Controls.Control.Avalonia.Controls.ISetLogicalParent.SetParent(ILogical parent)
   w Avalonia.Controls.Control.ClearLogicalParent(IEnumerable`1 children)
   w Avalonia.Controls.Control.LogicalChildrenCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   w Avalonia.Collections.AvaloniaList`1.NotifyRemove(IList t, Int32 index)
   w Avalonia.Collections.AvaloniaList`1.Remove(T item)
   w Avalonia.Controls.Presenters.ContentPresenter.UpdateChild()
   w Avalonia.Controls.Presenters.ContentPresenter.ApplyTemplate()
   w Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   w Avalonia.Layout.Layoutable.Measure(Size availableSize)
   w Avalonia.Layout.LayoutManager.Measure(ILayoutable control)
   w Avalonia.Layout.LayoutManager.ExecuteMeasurePass()
   w Avalonia.Layout.LayoutManager.ExecuteLayoutPass()
   w Avalonia.Threading.JobRunner.RunJobs()
   w Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   w Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   w Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken)
   w Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   w Avalonia.Application.Run(ICloseable closable)
   w Core2D.Avalonia.App.Start(IFileSystem fileIO, ILog log, ImmutableArray`1 writers) w C:\DOWNLOADS\GitHub-Core2D\Core2D\src\Core2D.Avalonia\App.xaml.cs:wiersz 181
          
    <StackPanel Background="Yellow">
        <TextBlock Text="Hello from Path" Classes="default"></TextBlock>
        <Rectangle Width="100" Height="50" Fill="Orange"/>
        <ListBox Name="listBox" Background="Red" VirtualizationMode="Simple">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Panel Background="Green"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem Background="Purple">Path</ListBoxItem>
        </ListBox>
    </StackPanel>

Using above Xaml: the ListBox is part of visual and logical tree, but its not rendered:

@wieslawsoltes have ben trying to look into this, but i'm unable to work out what a repro would look like in a sample project. Also i'm hoping #1032 might have fixed it. Was wondering if when you get a moment you could try it with latest Avalonia CI build and if it's still a problem, explain how to do a minimal repro?