Bug 33256 - Use of a simple control within a XAML-defined custom control when a class with the same name exists in the current namespace
Summary: Use of a simple control within a XAML-defined custom control when a class wit...
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2015-08-20 09:19 UTC by Filip Drsek
Modified: 2015-10-06 13:47 UTC (History)
7 users (show)

Tags: AC XAML
Is this bug a regression?: ---
Last known good build:


Attachments
Portable part of a simple Xamarin.Forms demonstration solution (295.52 KB, application/octet-stream)
2015-08-21 03:34 UTC, Filip Drsek
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Filip Drsek 2015-08-20 09:19:02 UTC
I have a Xamarin.Forms solution in Visual Studio 2015 on a Windows 7 machine with Xamarin 3.11.837.0 (Xamarin version as indicated in Control Panel – Programs – Programs and functions) and I try to run it on a Lenovo Yoga Tablet 2-1050F with Android 4.4.2. In my project named “MyControls” (Portable) in the “MyControls” namespace I need to define a class named “Button” and in the same namespace I define a compound custom control “ComboBox” using a “ComboBox.xaml” file and a “ComboBox.xaml.cs” file. I need to have a Xamarin.Forms.Button (not MyControls.Button) within this control, therefore in XAML I use “Button” without a namespace prefix and I expect “Button” to be taken from the namespace “http://xamarin.com/schemas/2014/forms”. I use the “x:Name” attribute to assign a name to the Button.

----- ComboBox.xaml -----
<?xml version="1.0" encoding="utf-8" ?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyControls.ComboBox">
...
  <Button Grid.Column="..." Grid.Row="..." x:Name="btnAddNew" ... />
</Grid>

When I build the project, in the “obj\Debug” folder the “ComboBox.xaml.g.cs” file has been generated:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyControls {
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    
    public partial class ComboBox : Grid {
        
        private Picker ComboBoxPicker;
        
        private Button btnAddNew;
        
        private void InitializeComponent() {
            this.LoadFromXaml(typeof(ComboBox));
            ComboBoxPicker = this.FindByName<Picker>("ComboBoxPicker");
            btnAddNew = this.FindByName<Button>("btnAddNew");
        }
    }
}

The generated file does not handle properly name ambiguity if I use a named control from the “Xamarin.Forms” namespace and my project defines in its namespace a class with the same name. It considers the object named “btnAddNew” to be from the “MyControls.Button” class, not from the “Xamarin.Forms.Button” class. The application then crashes on an exception, because it tries to cast the “Xamarin.Forms.Button” object to the type “MyControls.Button”:

08-20 14:53:01.299 I/MonoDroid(16096): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Cannot cast from source type to destination type.
08-20 14:53:01.299 I/MonoDroid(16096):   at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
08-20 14:53:01.299 I/MonoDroid(16096):   at Xamarin.Forms.NameScopeExtensions.FindByName[Button] (INameScope namescope, System.String name) [0x00007] in <filename unknown>:0 
08-20 14:53:01.299 I/MonoDroid(16096):   at Xamarin.Forms.NameScopeExtensions.FindByName[Button] (Xamarin.Forms.Element element, System.String name) [0x00000] in <filename unknown>:0 
08-20 14:53:01.299 I/MonoDroid(16096):   at MyControls.ComboBox.InitializeComponent () [0x00023] in C:\Users\filip.drsek\Documents\Visual Studio 2015\Projects\Test_CustomControls\MyControls\obj\Debug\ComboBox.xaml.g.cs:26 
08-20 14:53:01.299 I/MonoDroid(16096):   at MyControls.ComboBox..ctor () [0x0001d] in C:\Users\filip.drsek\Documents\Visual Studio 2015\Projects\Test_CustomControls\MyControls\ComboBox.xaml.cs:327 
08-20 14:53:01.299 I/MonoDroid(16096):   at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
08-20 14:53:01.299 I/MonoDroid(16096):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:535 
08-20 14:53:01.299 I/MonoDroid(16096):   --- End of inner exception stack trace ---
08-20 14:53:01.299 I/MonoDroid(16096):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:541 
08-20 14:53:01.299 I/MonoDroid(16096):   at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x000af] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System/Activator.cs:321 
08-20 14:53:01.299 I/MonoDroid(16096):   at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System/Activator.cs:214 
08-20 14:53:01.299 I/MonoDroid(16096):   at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, INode parentNode) [0x00184] in <filename unknown>:0
Comment 1 Atin 2015-08-20 14:17:50 UTC
I have tried to reproduce this issue and not able to reproduce this issue.

Screencast: http://www.screencast.com/t/hBFeGKBacZ

Please Let me know if I have missed anything.
Could you please provide us sample project? so that I can reproduce this sisue at our end.


Thanks..!
Comment 2 Filip Drsek 2015-08-21 03:34:04 UTC
Created attachment 12596 [details]
Portable part of a simple Xamarin.Forms demonstration solution

Button.cs: class with the same name as the “Button” class in the “Xamarin.Forms” namespace that I want to use in my custom control

CustomControl.xaml, CustomControl.xaml.cs: XAML-based definition of a custom control in which I want to use a “Xamarin.Forms.Button” instance

TestPage.xaml: a XAML page in which I instantiate my custom control

App.cs: using the “TestPage” page as the MainPage

I submit only the platform-independent project (portable) of the Xamarin.Forms solution to keep within the attachment size limit. I did not modify the platform-specific projects of the solution.
Comment 3 Filip Drsek 2015-08-21 03:52:04 UTC
In my demonstration project, I added the files “CustomControl.xaml” and “CustomControl.xaml.cs” (“ComboBox.xaml” and “ComboBox.xaml.cs” in the original solution) by right-clicking the portable project and selecting “Add – New Item... – Forms Xaml Page” and in both files that were created I replaced “ContentPage” by “Grid”.
Comment 4 Mohit Kheterpal 2015-08-21 12:59:53 UTC
I have tried to reproduce this issue with attached sample project in bug description and able to reproduce it. I am getting same exception mentioned in bug description. 

Screencast: http://www.screencast.com/t/u0Nb0OGHbA

Sample: https://www.dropbox.com/s/f4zq4wvjmvk0ok0/Test_NameCollision.zip?dl=0

DebugOutput: https://gist.github.com/Mohit-Kheterpal/3a8cf2fe346c11ef2390
IDE Log: https://gist.github.com/Mohit-Kheterpal/d9e6748ec0bed4908930

Environment info:
Microsoft Visual Studio Enterprise 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079
Installed Version: Enterprise
Xamarin   3.11.902.0
Xamarin.Android   5.1.4.42
Xamarin.iOS   8.10.5.0
Comment 5 Parmendra Kumar 2015-08-21 13:03:07 UTC
An update of comment comment #4,

I am getting same issue with Xamarin.Forms.1.5.0.6396-pre1.
Comment 6 Filip Drsek 2015-09-02 07:54:03 UTC
There is also a problem if a XAML-based control is defined in a namespace that has “Xamarin” as one part of its name, for example “MySolution.Xamarin.Controls”. In this case the auto-generated file cannot be compiled, because the namespace “Xamarin.Forms” used in this file is wrongly interpreted, for example as “MySolution.Xamarin.Forms”.
Comment 7 Filip Drsek 2015-09-07 04:06:21 UTC
These problems would be solved if the namespaces in the “*.g.cs” files had the prefix “global::”. In the discussion “ http://stackoverflow.com/questions/3552763/why-use-the-global-keyword-in-c ” one participant says, “It is best to use the global namespace prefix in generated code.” However, this recommendation is not followed in the generated code of Xamarin.Forms XAML-based pages.
Comment 8 Filip Drsek 2015-09-07 04:29:45 UTC
Now I see that in “*.xaml.g.cs” files the “global::” prefix already appears in the declarations of controls and in their assignment with the “FindByName” method, but still not in the “using” clauses.
Comment 9 Jason Smith [MSFT] 2015-09-24 13:37:22 UTC
Should be fixed in 1.5.1-pre1
Comment 10 Parmendra Kumar 2015-10-06 13:47:49 UTC
I have checked this issue with Xamarin.Forms 1.5.1-pre1 and its working fine.

Hence closing this issue.