maui icon indicating copy to clipboard operation
maui copied to clipboard

Unexpected exception from VisualTreeElementExtensions.GetVisualTreeElements

Open etvorun opened this issue 1 year ago • 1 comments

Description

Element selection does not work in VS XAML Live Preview when XAML contains Border with ImageButton.

Steps to Reproduce

  1. Create new MAUI .NET8 app.
  2. Open MainWindow.xaml and replace VerticalLayout with
<VerticalStackLayout>
	<Button Text="Hit test 100x200" Clicked="Button_Clicked"/>
	<Label x:Name="HitTestLabel"/>
	<Border>
		<ImageButton Source="dotnet_bot" HeightRequest="50" WidthRequest="65"/>
	</Border>
</VerticalStackLayout>
  1. Open MainPage.xaml.cs and add
private void Button_Clicked(object sender, EventArgs e)
{
    if (App.Current!.Windows.FirstOrDefault() is Window window)
    {
        IReadOnlyList<IVisualTreeElement> elements = VisualTreeElementExtensions.GetVisualTreeElements(window, 100, 200);
        this.HitTestLabel.Text = $"Hit test element {elements?.Count ?? 0}";
    }
    else
    {
        this.HitTestLabel.Text = $"No window to hit test";
    }
}
  1. Build and run (a.k.a. F5).
  2. Open Debug > Windows > XAML Live Preview window.
  3. Move mouse over elements in the XLP window Expected: elements get highlighted with outline border Actual: elements don't get highlighted Note: if you switch to running app and click on "Hit Test" button the app will crash with the call stack below pointing to VisualTreeElementExtensions.GetVisualTreeElements. Note that XLP uses that API for element selection.

System.InvalidOperationException: 'MauiContext should have been set on parent.' Microsoft.Maui.dll!Microsoft.Maui.Platform.ElementExtensions.ToPlatform(Microsoft.Maui.IElement view) Unknown Microsoft.Maui.dll!Microsoft.Maui.VisualTreeElementExtensions.GetVisualTreeElementsWindowsInternal.AnonymousMethod__7_1(Microsoft.Maui.IVisualTreeElement n) Unknown System.Linq.dll!System.Linq.Enumerable.WhereSelectListIterator<Microsoft.Maui.IVisualTreeElement, System.Tuple<Microsoft.Maui.IView, object>>.MoveNext() Unknown System.Linq.dll!System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Tuple<Microsoft.Maui.IView, object>, Microsoft.Maui.IVisualTreeElement>.MoveNext() Unknown System.Private.CoreLib.dll!System.Collections.Generic.List<Microsoft.Maui.IVisualTreeElement>.AddRange(System.Collections.Generic.IEnumerable<Microsoft.Maui.IVisualTreeElement> collection) Unknown Microsoft.Maui.dll!Microsoft.Maui.VisualTreeElementExtensions.GetVisualTreeElementsWindowsInternal(Microsoft.Maui.IVisualTreeElement visualElement, System.Func<Microsoft.UI.Xaml.UIElement, System.Collections.Generic.IEnumerable<Microsoft.UI.Xaml.UIElement>> findChildren) Unknown Microsoft.Maui.dll!Microsoft.Maui.VisualTreeElementExtensions.GetVisualTreeElements(Microsoft.Maui.IVisualTreeElement visualElement, Microsoft.Maui.Graphics.Point point) Unknown Microsoft.Maui.dll!Microsoft.Maui.VisualTreeElementExtensions.GetVisualTreeElements(Microsoft.Maui.IVisualTreeElement visualElement, double x, double y) Unknown MauiApp1.dll!MauiApp1.MainPage.Button_Clicked(object sender, System.EventArgs e) Line 28 Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked() Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ButtonElement.ElementClicked(Microsoft.Maui.Controls.VisualElement visualElement, Microsoft.Maui.Controls.Internals.IButtonElement ButtonElementManager) Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.SendClicked() Unknown Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() Unknown Microsoft.Maui.dll!Microsoft.Maui.Handlers.ButtonHandler.OnClick(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Unknown Microsoft.WinUI.dll!WinRT._EventSource_global__Microsoft_UI_Xaml_RoutedEventHandler.EventState.GetEventInvoke.AnonymousMethod__1_0(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) Unknown Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(nint thisPtr, nint sender, nint e) Unknown [Native to Managed Transition] [Managed to Native Transition] Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.Controls.IControlOverridesMethods.OnPointerReleased(WinRT.IObjectReference _obj, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Controls.Control.OnPointerReleased(Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Controls.Control.Microsoft.UI.Xaml.Controls.IControlOverrides.OnPointerReleased(Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e) Unknown Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.Controls.IControlOverrides.Do_Abi_OnPointerReleased_3(nint thisPtr, nint e) Unknown [Native to Managed Transition] [Managed to Native Transition] Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.IApplicationStaticsMethods.Start(WinRT.IObjectReference _obj, Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Unknown Microsoft.WinUI.dll!Microsoft.UI.Xaml.Application.Start(Microsoft.UI.Xaml.ApplicationInitializationCallback callback) Unknown MauiApp1.dll!MauiApp1.WinUI.Program.Main(string[] args) Line 31

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

etvorun avatar Jan 04 '24 22:01 etvorun

Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds

PureWeen avatar Jan 04 '24 23:01 PureWeen

@etvorun https://github.com/dotnet/maui/commit/bb90908da186a979e02286ca91e62b31892bc00c

This is not in a public release yet. It should work with Main builds.

e. I checked and it's in https://github.com/dotnet/maui/pull/19381

Once that lands, it should be in the service release, whenever that ships.

drasticactions avatar Jan 05 '24 01:01 drasticactions