Search code examples
macosxamlbuttonxamarin.formsvisual-studio-mac

Visual Studio for Mac will not create a button click handler


I have seen this question but in my case when I type Clicked="" and choose the option to create the handler I see this:

enter image description here

It won't let me create a handler. 🧐

Code snippet:

<RelativeLayout HorizontalOptions="FillAndExpand">
    <Button x:Name="btnAddElder" Text="Add" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.0000, Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.3333, Constant=0}" />
    <Button x:Name="btnEditElder" Text="Edit" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.3333, Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=.3333,Constant=0}" />
    <Button x:Name="btnDeleteElder" Text="Delete" RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.6666, Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.3333, Constant=0}" />
</RelativeLayout>

Thanks for your advice.


Solution

  • The reason for the problem appears to be the way I had named my Button elements. They all started with a lowercase letter.

    When I use names which start with an uppercase letter, like:

     <RelativeLayout HorizontalOptions="FillAndExpand">
            <Button
                x:Name="AddPublisherButton"
                Padding="10,10,10,10"
                RelativeLayout.WidthConstraint="{ConstraintExpression Constant=0,
                                                                      Factor=.3333,
                                                                      Property=Width,
                                                                      Type=RelativeToParent}"
                RelativeLayout.XConstraint="{ConstraintExpression Constant=0,
                                                                  Factor=.0000,
                                                                  Property=Width,
                                                                  Type=RelativeToParent}"
                Text="Add"
                Clicked="AddPublisherButton_Clicked"/>
            <Button
                x:Name="EditPublisherButton"
                Padding="10,10,10,10"
                RelativeLayout.WidthConstraint="{ConstraintExpression Constant=0,
                                                                      Factor=.3333,
                                                                      Property=Width,
                                                                      Type=RelativeToParent}"
                RelativeLayout.XConstraint="{ConstraintExpression Constant=0,
                                                                  Factor=.3333,
                                                                  Property=Width,
                                                                  Type=RelativeToParent}"
                Text="Edit"
                Clicked="EditPublisherButton_Clicked"/>
            <Button
                x:Name="DeletePublisherButton"
                Padding="10,10,10,10"
                RelativeLayout.WidthConstraint="{ConstraintExpression Constant=0,
                                                                      Factor=.3333,
                                                                      Property=Width,
                                                                      Type=RelativeToParent}"
                RelativeLayout.XConstraint="{ConstraintExpression Constant=0,
                                                                  Factor=.6666,
                                                                  Property=Width,
                                                                  Type=RelativeToParent}"
                Text="Delete"
                Clicked="DeletePublisherButton_Clicked"/>
        </RelativeLayout>
    

    Then it does create the handlers! See:

    void AddElderButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }
    
    void EditElderButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }
    void DeleteElderButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }
    
    void AddPublisherButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }
    
    void EditPublisherButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }
    
    void DeletePublisherButton_Clicked(System.Object sender, System.EventArgs e)
    {
    }