Below I describe how to reproduce an error I'm receiving. It behaves the same in VS 2010, 2012, and 2013.
Steps to reproduce the error:
1. Create a solution.
2. Create a C# class library called Common, containing one file named Handler.cs:
using System;
namespace Common
{
public delegate void Handler(object sender, EventArgs args);
}3. Create a WPF user control library project called MyControlLibrary, referencing Common. In it, create a user control called MyControl.xaml.
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;
}
}4. Create a WPF Application project called MyWpfApplication, referencing Common and MyControlLibrary. In it, create WindowNamespace.Common.cs as well as a window called MyWindow.xaml.
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)
{
}
}
}5. Build the solution.
You should receive the following error: "The type or namespace name 'Handler' does not exist in the namespace 'WindowNamespace.Common' (are you missing an assembly reference?)". The compiler points to line 7 of MyWindow.xaml:
<c:MyControl MyEvent="MyControl_MyEvent" />
If you open the .g.i.cs 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 issue is that it is trying to find Common.Handler in WindowNamespace. This could be resolved by having it generated as:
#line 7 "..\..\MyWindow.xaml" ((ControlNamespace.MyControl)(target)).MyEvent += new global::Common.Handler(this.MyControl_MyEvent);
Or by adding a using to the top of the file:
using Common; ... #line 7 "..\..\MyWindow.xaml" ((ControlNamespace.MyControl)(target)).MyEvent += new Handler(this.MyControl_MyEvent);
Is this actually a bug in the XAML->.NET translation, or is there something I am doing wrong?