Have an interesting situation which I do not understand.
Here's the scenario:
I create a simple Silverlight 5 application. I place a button control on the main page and create a Click event handler.
XAML is shown below.....
<UserControl x:Class="SilverlightApplication6.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="130,43,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</UserControl>
So far so good.
Wire up an event handler in code behind as so ....
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hi There!!!");
}
Compile and run the app. I 'Click' the button and get a nice little message box. Again, so far, so good.
Add a new method (as shown below) to the code behind with the same method name as the button click event handler but with a different signature.
private void button1_Click()
{
MessageBox.Show("This method causes a XamlParseException!");
}
Compile and execute the code. Bring up the app and click on the button and I get the following exception:
System.Windows.Markup.XamlParseException was unhandled by user code
Message=Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'. [Line: 10 Position: 149]
LineNumber=10
LinePosition=149
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at SilverlightApplication6.MainPage.InitializeComponent()
at SilverlightApplication6.MainPage..ctor()
at SilverlightApplication6.App.Application_Startup(Object sender, StartupEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
InnerException:
Why does this happen?
The problem is that
void button1_Click()
is not a valid handler for theClick
event.Click
is aRoutedEventHandler
, which is defined as:Note that you can also use
void button1_Click(object anyName, EventArgs forTheParams)
, it just has to match the delegate closely enough to be converted.I had the scenario where I linked a button to a method like this...
I somehow duplicated the method without knowing and as a result I also got this error. Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click Hope this can help someone save some time.