4

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?

doon386
  • 41
  • 1
  • 4

2 Answers2

2

The problem is that void button1_Click() is not a valid handler for the Click event. Click is a RoutedEventHandler, which is defined as:

public delegate void RoutedEventHandler(
    Object sender,
    RoutedEventArgs e
)

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.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • 1
    Correct, it's not a valid handler -- but I'm not trying to use it as such. The mere presence of this code generates the exception. – doon386 Jun 19 '12 at 18:11
  • Hm...obvious workaround is to rename one of the methods. Another possible option is to add parameter defaults of null, (`object sender = null, EventArgs e = null`) so that your code can still call `button1_Click()`, and it just has null sender and e params. If these don't work, an explanation of the relationship between these two overloads would be nice. – Tim S. Jun 19 '12 at 19:21
  • 1
    I don't need a work around. That's easy, as you suggested, just rename the method. What I'm curious about is, given the polymorphic nature of C#/.NET/CLR, why the proper method cannot be resolved/determined at run time? – doon386 Jun 19 '12 at 19:59
  • I have the same problem, but I believe XamlParser is using reflection to search methods with the name of the handler that is set in xaml. If it finds more than one, throws the exception. – Jone Polvora Jan 26 '13 at 04:11
0

I had the scenario where I linked a button to a method like this...

<Button Click="MethodName"..../>

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.

Stephan
  • 89
  • 2
  • 5