Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

VisualTreeHelper.FindElementsInHostCoordinates() returns different subtree values for controls on Canvas ( UWP vs WASM) #4073

Open
19 tasks
ToddGlodek opened this issue Sep 19, 2020 · 5 comments
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/pointers 🖱️ Categorizes an issue or PR as relevant to mouse/touch/pen pointers project/shapes-brushes 🔶 Categorizes an issue or PR as relevant to shapes and brushes

Comments

@ToddGlodek
Copy link

I have code that calls VisualTreeHelper.FindElementsInHostCoordinates() in to return all the controls beneath pointer when OnPointerPressed() is triggered. The same code produces different results when complied as UWP vs WASM (even if you hard code the value of pagePoint to ensure that it its with a particular control).

var elementsAtPosit = VisualTreeHelper.FindElementsInHostCoordinates(pagePoint, theCanvas);

Current behavior

I have a Uno.Shared project which features a Canvas and a bunch of UserControls (Circles & Squares). I have written a work around to the UNO drag and drop limitations that I can use to reposition all the various Circles and Squares on the parent Canvas. When the code runs as UWP, it returns the desired control subtree (all the appropriate overlapping Circles and Squares plus the Canvas control). When running the WASM code, only the Canvas control is returned.

Expected behavior

The same list of controls should be returned by both UWP and WASM version of the code.

How to reproduce it (as minimally and precisely as possible)

  1. Create a project page that features a Canvas control
  2. Place some content (eg: Circles & Squares) on the Canvas
  3. Implement an event handler for OnPointerPressed(PointerRoutedEventArgs e) that captures the 'click'
  4. Within the event handler call VisualTreeHelper.FindElementsInHostCoordinates() to get controls at specified x-y coordinate
  5. Write the resulting list of controls to the log/debuger trace.
  6. Run the code on both UWP/WASM and then click on the some of the Circles & Squares
  7. Compare the logs.
  8. Observe that the list of controls is different between UWP &WASM

Workaround

  1. Because I am working with custom classes derived from UserControl I was able to do the following:

  2. I made a subclass of the view model especially for WASM that contained an ObservableCollection

  3. In myControl I added event handlers for PointerEntered() & PointerExited() that added/removed items from the collection in view model

    private IEnumerable GetUIElementsAtPointerPosition(PointerRoutedEventArgs e)
    {
    var rtnList = new List();

         Point pagePoint = GetPagePoint(e);
    

#if ! WASM COMPILER DIRECTIVE*/
var elementsAtPosit = VisualTreeHelper.FindElementsInHostCoordinates(pagePoint, theCanvas);
#else

        var elementsAtPosit = (this.DataContext as DrawingSurfaceViewModelWASM).ControlsBeneathPointer.AsEnumerable();

#endif
...
}

This technique produced the desired list of controls

Environment

Nuget Package:

  • [X ] Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia
  • [X ] Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia
  • Uno.SourceGenerationTasks
  • Uno.UI.RemoteControl / Uno.WinUI.RemoteControl
  • Other:

Nuget Package Version(s):

Affected platform(s):

  • iOS
  • Android
  • [X ] WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Skia
    • WPF
    • GTK (Linux)
    • Tizen
  • [X ] Windows
  • Build tasks
  • Solution Templates

IDE:

  • Visual Studio 2017 (version: )
  • [X ] Visual Studio 2019 (version: )
  • Visual Studio for Mac (version: )
  • Rider Windows (version: )
  • Rider macOS (version: )
  • Visual Studio Code (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

@ToddGlodek ToddGlodek added kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Sep 19, 2020
@jeromelaban jeromelaban removed the triage/untriaged Indicates an issue requires triaging or verification label Sep 21, 2020
@agneszitte agneszitte added the project/shapes-brushes 🔶 Categorizes an issue or PR as relevant to shapes and brushes label Sep 21, 2020
@jeromelaban jeromelaban added the project/pointers 🖱️ Categorizes an issue or PR as relevant to mouse/touch/pen pointers label Sep 23, 2020
@ToddGlodek
Copy link
Author

ToddGlodek commented Oct 10, 2020

I noticed that the code I wrote no longer works the way it did previously - yet VisualTreeHelper.cs does not appears to have changed since Mar 20.

Would something have changed recently beneath the covers somewhere to alter the behavior of FindElementsInHostCoordinates(). The enumeration is now empty on UWP as well.

I believe at the time I was using :

    <PackageReference Include="Uno.UI" Version="3.1.0-dev.465" />
    <PackageReference Include="Uno.UI.RemoteControl" Version="3.1.0-dev.465" Condition="'$(Configuration)'=='Debug'" />
    <PackageReference Include="Uno.UI.WebAssembly" Version="3.1.0-dev.465" />

Whereas now I am using

<PackageReference Include="Uno.UI" Version="3.1.0-dev.739" />
<PackageReference Include="Uno.UI.RemoteControl" Version="3.1.0-dev.739" Condition="'$(Configuration)'=='Debug'" />
<PackageReference Include="Uno.UI.WebAssembly" Version="3.1.0-dev.739" />
<PackageReference Include="Uno.Wasm.Bootstrap" Version="1.5.0-dev.57" />
<PackageReference Include="Uno.Wasm.Bootstrap.DevServer" Version="1.5.0-dev.57" />

Strange ??

@davidjohnoliver
Copy link
Contributor

If the enumeration is empty on UWP, the difference is most likely in your own code - Uno is not used when running on UWP.

@jeromelaban jeromelaban added the difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. label Feb 15, 2021
@MartinZikmund MartinZikmund added difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI and removed difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Jun 2, 2021
@MartinZikmund
Copy link
Member

@ToddGlodek is this still an issue for you?

@MartinZikmund MartinZikmund added the triage/needs-information Indicates an issue needs more information in order to work on it. label Jun 2, 2021
@ToddGlodek
Copy link
Author

ToddGlodek commented Jun 2, 2021 via email

@no-response no-response bot removed the triage/needs-information Indicates an issue needs more information in order to work on it. label Jun 2, 2021
@ToddGlodek
Copy link
Author

I suspect that the underling cause for this issue will turn out to have been
Loading, Loaded & Unloaded events are called too early #2895. I documented, what feels to be a very similar set of symptoms, as part of Page Loading Fails - Potentially because 2 nested XAML controls in a direct parent/child get instantiated/InitializeComponented/loaded in different order between WASM / WinUI #10276. I believe #2895 is whats causing this behavior too.

@MartinZikmund MartinZikmund changed the title VisualTreeHelper.FindElementsInHostCoordinates() returns different subtree values for controls on Canvas ( UWP vs WASM) VisualTreeHelper.FindElementsInHostCoordinates() returns different subtree values for controls on Canvas ( UWP vs WASM) Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/pointers 🖱️ Categorizes an issue or PR as relevant to mouse/touch/pen pointers project/shapes-brushes 🔶 Categorizes an issue or PR as relevant to shapes and brushes
Projects
None yet
Development

No branches or pull requests

5 participants