xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.88k forks source link

[Bug] Setting x:Name="root" on an element inside of a DataTemplate breaks event handlers. #14385

Open sjordanGSS opened 3 years ago

sjordanGSS commented 3 years ago

Description

If x:Name is set to "root" on an element inside of a DataTemplate (e.g. in the ItemTemplate of a CollectionView), any events tied to elements inside of the "root" element will be invoked with the 'this' pointer set to the "root" element, instead of the class on which the event handler is defined. At this point, any parts of the owning class that are used in the event handler (e.g. trying to access Resources) will be called against "root", resulting in exceptions if they don't exist.

Steps to Reproduce

A reproduction is available here: https://github.com/sjordanGSS/xNameRootBug

  1. Tap the Button labelled "tap me!"

Expected Behavior

Event handler "Button_Clicked" in MainPage.xaml.cs should be invoked, outputting the text "Clicked called from xNameRootBug.MainPage" to the debug output

Actual Behavior

Event handler "Button_Clicked" in MainPage.xaml.cs is invoked, but the debug output text reads "Clicked called from Xamarin.Forms.StackLayout". Placing a breakpoint here and inspecting 'this' in the Locals window of Visual Studio reveals that it is set to the "root" StackLayout instead of MainPage.

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Community 2019 Version 16.10.3 VisualStudio.16.Release/16.10.3+31424.327 Microsoft .NET Framework Version 4.8.04084 Installed Version: Community ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 2019 16.10.526.50910 ASP.NET and Web Tools 2019 ASP.NET Web Frameworks and Tools 2019 16.10.526.50910 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 16.10.526.50910 Azure App Service Tools v3.0.0 Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer. Azure Data Lake Tools for Visual Studio 2.6.1000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 16.10.526.50910 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.1000.0 Microsoft Azure Stream Analytics Tools for Visual Studio C# Tools 3.10.0-4.21318.11+7ceb633154acb9d716fd3eb2b6df1a0468d8e416 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Fabric.DiagnosticEvents 1.0 Fabric Diagnostic Events IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info Microsoft Azure HDInsight Azure Node 2.6.1000.0 HDInsight Node under Azure Node Microsoft Azure Hive Query Language Service 2.6.1000.0 Language service for Hive query Microsoft Azure Service Fabric Tools for Visual Studio 16.10 Microsoft Azure Service Fabric Tools for Visual Studio Microsoft Azure Stream Analytics Language Service 2.6.1000.0 Language service for Azure Stream Analytics Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.40423.1 Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Microsoft Library Manager 2.1.113+g422d40002e.RR Install client-side libraries easily to any web project Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers Microsoft Visual Studio Tools for Containers 1.2 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container. Mono Debugging for Visual Studio 16.10.15 (552afdf) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 5.10.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Razor (ASP.NET Core) 16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 16.0.62106.24090 Microsoft SQL Server Data Tools ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 16.0.30429.2002 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.10.0-4.21318.11+7ceb633154acb9d716fd3eb2b6df1a0468d8e416 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 16.10.0-beta.21262.7+1b23bbeda88ea3cb9be9af777f4c99fa8663df81 Microsoft Visual F# Tools Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers Visual Studio Tools for Kubernetes 1.0 Visual Studio Tools for Kubernetes VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Foo 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Xamarin 16.10.000.234 (d16-10@ecaf29b) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.10.0.115 (remotes/origin/c750fbf1bde3c720d077f51640fe197c6dac7cbe@c750fbf1b) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.10.5 (355b57a) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.3.0.4 (d16-10/ae14caf) Xamarin.Android Reference Assemblies and MSBuild support. Mono: b4a3858 Java.Interop: xamarin/java.interop/d16-10@f39db25 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-10@c5732a0 Xamarin.iOS and Xamarin.Mac SDK 14.20.0.24 (c4b89cddb) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Reproduction Link

https://github.com/sjordanGSS/xNameRootBug A few notes on the repro: The CollectionView being named has no effect on the bug, it is just done so that ItemsSource can be populated from code-behind. Also, "this.GetType()" in the event handler can be replaced with "GetType()" with no change in behaviour.

Workaround

avoid using "root" as a value for x:Name

sjordanGSS commented 3 years ago

Quick update on this bug report: the bug is still present in Xamarin.Forms 5.0.0sr4, but I've made an app to test this in Maui (built using Preview 6 in Visual Studio 2019) and the bug does not appear to be present there. Test app is here: https://github.com/sjordanGSS/XNameRootMaui. Note that when pressing the "Tap Me!" button, the 'this' pointer inside the click event handler is correctly set to MainPage instead of the StackLayout with x:Name="root" set.