How do you bind button click events created in C # code using methods?

I am creating a Windows Phone application and I cannot create a dynamically created Button with its event handler.

Code I use:

public partial class MainPage : PhoneApplicationPage
{
    Button AddButton

    public MainPage()
    {
        CreateInterestEntry();
    }

    private void CreateInterestEntry()
    {
        EntrySegment = getEntrySegment();
        ContentPanel.Children.Add(EntrySegment);
    }

    private void AddButton_Click(Object sender, RoutedEventArgs e)
    {
        //Stuff to do when button is clicked
    }

    private Grid getEntrySegment()
    {
        Grid EntrySegment = new Grid();

       //Creating the grid goes here

        AddButton = new Button();
        AddButton.Content = "Add";
        AddButton.Click += new EventHandler(AddButton_Click);

        return EntrySegment;
    }
}

}

Using this code, he complains that no overload for AddButton_Click matches the delegate "System.EventHandler".

It is almost identical to the code under the examples here in msdn, except that I changed the argument type in AddButton_Click from EventArgs to RoutedEventArgs since otherwise Visual Studio tells me that it cannot implicitly convert from System.EventHandler to System.RoutedEventHandler.

Thanks in advance

+3
source share
1

:

AddButton.Click += new RoutedEventHandler(AddButton_Click);

Click:

void AddButton_Click(object sender, RoutedEventArgs e)
{
}
+2

All Articles