Is this a namespace collision due to an error in generating XAML code on .NET?

Below I describe how to reproduce the error I get. It behaves the same in VS 2010, 2012 and 2013. Dividing it into several projects, as I indicate below, is important.

Steps to reproduce the error:

  • Create a solution.

  • Create a C # class library called Common, containing one file called Handler.cs:

    using System; namespace Common { public delegate void Handler(object sender, EventArgs args); } 
  • Create a WPF user control library project called MyControlLibrary, referencing Common. In it, create a custom MyControl.xaml control.

    MyControl.xaml:

     <UserControl x:Class="ControlNamespace.MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> </Grid> </UserControl> 

    MyControl.xaml.cs:

     using System.Windows.Controls; using Common; namespace ControlNamespace { public partial class MyControl : UserControl { public MyControl() { InitializeComponent(); } public event Handler MyEvent; } } 
  • Create a WPF application project called MyWpfApplication, referencing Common and MyControlLibrary. In it, create WindowNamespace.Common.cs, as well as the MyWindow.xaml window.

    WindowNamespace.Common.cs:

     namespace WindowNamespace.Common { } 

    MyWindow.xaml:

     <Window x:Class="WindowNamespace.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:ControlNamespace;assembly=WpfControlLibrary1" Title="MyWindow" Height="300" Width="300"> <Grid> <c:MyControl MyEvent="MyControl_MyEvent" /> </Grid> </Window> 

    MyWindow.xaml.cs:

     using System; using System.Windows; namespace WindowNamespace { public partial class MyWindow : Window { public MyWindow() { InitializeComponent(); } void MyControl_MyEvent(object sender, EventArgs args) { } } } 
  • Create a solution.

You should get the following error by pointing to line 7 of MyWindow.xaml:

The type or name of the namespace "Handler" does not exist in the namespace 'WindowNamespace.Common' (do you miss the assembly reference?)

If you open the .gics file generated for MyWindow.xaml, you should see the following in the IComponentConnector.Connect method:

 #line 7 "..\..\MyWindow.xaml" ((ControlNamespace.MyControl)(target)).MyEvent += new Common.Handler(this.MyControl_MyEvent); 

The source of the problem is that he is trying to find Common.Handler in WindowNamespace. This could be solved by creating it as:

 #line 7 "..\..\MyWindow.xaml" ((ControlNamespace.MyControl)(target)).MyEvent += new global::Common.Handler(this.MyControl_MyEvent); 

Or adding use to the top of the file:

 using Common; ... #line 7 "..\..\MyWindow.xaml" ((ControlNamespace.MyControl)(target)).MyEvent += new Handler(this.MyControl_MyEvent); 

Please note that if all these source files are combined into one project, the error disappears because the .gics file is generated differently (i.e. it does not explicitly add a handler to the event).

Is this really a mistake in translating XAML β†’. NET, or is there something I'm doing wrong?

+6
source share
1 answer

There is a hint about what happens if you override the handler delegate in your WindowNamespace.Common namespace declaration in MyWpfApplication and try to compile:

 Error 1 Cannot implicitly convert type 'WindowNamespace.Common.Handler' to 'Common.Handler' c:\Dev\YourSolution\MyWpfApplication\MyWindow.xaml 7 63 MyWpfApplication 

A common project declares a global namespace called "Common", which your management library also uses. But when your application explicitly declares "WindowNamespace.Common", it creates a local namespace, which is simply the same namespace that the parent control is in. This effectively creates the ambiguity problem described in Visual Studio Documentation for CS0433 Compiler Error .

Change the general namespace declaration in MyWpfApplication to just be "Common" and the problem goes away.

0
source

All Articles