Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] VisualTreeChangeEventArgs, Parent is null when element is removed and VisualTreeChangeType is Add instead of Remove and ChildIndex is -1 #11334

Closed
chabiss opened this issue Jul 7, 2020 · 1 comment
Assignees
Labels
external-hotreload Non-Forms bugs that affect Hot Reload in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Milestone

Comments

@chabiss
Copy link

chabiss commented Jul 7, 2020

Description

Steps to Reproduce

  1. Launch the attached application
    XamarinTest.zip
  2. Clear you debug output to see the VisualTreeChangeEventArgs
  3. Pressed the Removed Button which is going to remove the first label at 25:10 from the stack panel
  4. Pay attention to the debug output

Expected Behavior

VisualTreeChangeEventArgs Remove:
Parent:Xamarin.Forms.StackLayout: MainPage.xaml;assembly=XamarinTest:23:6-->
ChildIndex: 0: Child:Xamarin.Forms.Label: MainPage.xaml;assembly=XamarinTest:25:10

Actual Behavior

VisualTreeChangeEventArgs Add:
Parent:null: ::-->
ChildIndex: -1: Child:Xamarin.Forms.Label: MainPage.xaml;assembly=XamarinTest:25:10

Basic Information

The parent is null, this is not good. Also, the ChildIndex is -1 which is also not good.

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:
    • iOS:
    • Android:
    • UWP:
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

Workaround

VS bug #1152707, VS bug #1152813

@chabiss chabiss added s/unverified New report that has yet to be verified t/bug 🐛 labels Jul 7, 2020
@chabiss chabiss changed the title [Bug] VisualTreeChangeEventArgs, Parent is null when element is removed and VisualTreeChangeType is Add instead of Remove [Bug] VisualTreeChangeEventArgs, Parent is null when element is removed and VisualTreeChangeType is Add instead of Remove and ChildIndex is -1 Jul 7, 2020
@jsuarezruiz jsuarezruiz added the external-hotreload Non-Forms bugs that affect Hot Reload label Jul 7, 2020
@StephaneDelcroix StephaneDelcroix self-assigned this Jul 7, 2020
@StephaneDelcroix StephaneDelcroix removed the s/unverified New report that has yet to be verified label Jul 8, 2020
@maxbrister
Copy link
Contributor

@StephaneDelcroix Here's a description of what we expect. Please let me know if I'm missing anything.

Goal: VS needs to maintain and display a tree representing the application's visible[1] UI elements. VS uses VisualTreeChangeEvents to update its internal representation of the visual tree.

  • VisualTreeChangeEventArgs.Parent - Determines the node in the Visual Tree that has changed. Each node contains an ordered array of children. If Parent is null then this is a "root" element.
  • VisualTreeChangeEventArgs.Child - The node being inserted or removed. Should be specified for both add and remove, because VS will check the underlying data structure for consistency.
  • VisualTreeChangeEventArgs.ChangeType - Indicates the operation is either an Insert or Remove. (VisualTreeChangeType.Add was an unfortunate naming choice - really it's an insert)

Pseudo code for how VS interprets ChangeType == VisualTreeChangeType.Add

var parent = GetNode(args.Parent);
var child = GetNode(args.Child);
parent.Children.Insert(args.ChildIndex, child);

Pseudo code for how VS interprets ChangeType == VisualTreeChangeType.Remove

var parent = GetNode(args.Parent);
parent.Children.RemoveAt(args.ChildIndex);

Wpf and Uwp treat initial registration for VisualTreeChangeEvents differently. Either way is acceptable.

  • Uwp Model - On registration a flood of VisualTreeChangeEvents with ChangeType == Add is sent to describe the current tree. If a UI element is removed from the Visual Tree a single Remove event is sent. If a new element is added to the Visual Tree a flood of events is sent describing the new element an all of its children are sent.
  • Wpf Model - No events are automatically sent on attach. VisualTreeHelper is used to discover the initial tree. When a new element is added to the visual tree a single VisualTreeEvent with VisualTreeChangeType.Add is sent. VisualTreeHelper is used again to discover the shape of the new subtree. This approach relies on being able to accurately determine the VisualTree outside of VisualTreeChangeEvents, which we don't know how to do in Xamarin. This is what the Xamarin tap is currently based on.

[1] Both Wpf and Uwp include elements with Visiblity==Collapsed if they would otherwise be visible

StephaneDelcroix added a commit that referenced this issue Aug 13, 2020
StephaneDelcroix added a commit that referenced this issue Aug 13, 2020
    [C] send index on VTChange.Add
    [C] register LV cells as LogicalChildren
    [C] add index in VTChanges.Remove

    - fixes #11334
    - fixes #11335
    - fixes AB#1152749
    - fixes AB#1152814
@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label Aug 13, 2020
@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
StephaneDelcroix added a commit that referenced this issue Aug 14, 2020
    [C] send index on VTChange.Add
    [C] register LV cells as LogicalChildren
    [C] add index in VTChanges.Remove

    - fixes #11334
    - fixes #11335
    - fixes AB#1152749
    - fixes AB#1152814
@samhouts samhouts modified the milestones: 5.0.0, 4.8.0 Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
external-hotreload Non-Forms bugs that affect Hot Reload in-progress This issue has an associated pull request that may resolve it! t/bug 🐛
Projects
None yet
Development

No branches or pull requests

5 participants