Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'

笑着哭i 提交于 2019-12-04 04:20:16

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/11106618/failed-to-assign-to-property-system-windows-controls-primitives-buttonbase-clic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!