Button Clicked event fired twice when only pressed once

I'm trying to create my first app, it's kind of a copy of the Tasky demo app, but with some custom features. I'm running into a problem that I don't really understand.

This is my XAML form:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EasyNotes.UI.NewNotePage">
  <ScrollView>
    <StackLayout>
      <Label Text="Title"/>
      <Entry x:Name="entryTitle" Placeholder="Type the title here"/>
      <Label Text="Description"/>
      <Editor x:Name="editorDescription" HeightRequest="150"/>
      <Button x:Name="buttonSave" Text="Save" Clicked="buttonSave_Clicked"/>
    </StackLayout>
  </ScrollView>
</ContentPage>

This is the code behind:

public partial class NewNotePage : ContentPage
    {
        public NewNotePage()
        {
            InitializeComponent();

            buttonSave.Clicked += buttonSave_Clicked;
        }

        public void buttonSave_Clicked(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(entryTitle.Text) || string.IsNullOrWhiteSpace(editorDescription.Text))
            {
                DisplayAlert("Alert", "All fields are required.", "OK");
            }
            else
            {
                Note note = new Note();

                note.Title = entryTitle.Text;
                note.Description = editorDescription.Text;
                note.DateAndTime = DateTime.Now;
                note.IsActive = true;

                NoteFactory factory = new NoteFactory();

                bool isSaved = factory.AddNewNote(note);
                if (isSaved)
                {
                    DisplayAlert("Alert", "Note saved sucessfully.", "OK");
                }
                else
                {
                    DisplayAlert("Alert", "Note was not saved.", "OK");
                }

                Navigation.PopAsync();
            }
        }
    }

When I press the buttonSave the buttonSave_Clicked method it's called twice and my Note object it's saved duplicated to the database. I find strange that when I'm debugging this piece of code the DisplayAlert doesn't show on the first time and after the code run for the second time it shows again, instead both alerts are displayed consecutively in the end, after the second run.

Any ideas on why this is happening and what do I need to change in order to make it work?

Best Answer

  • JulienRosenJulienRosen CA ✭✭✭✭
    edited May 2016 Accepted Answer
    <Button Clicked="buttonSave_Clicked"/>
    buttonSave.Clicked += buttonSave_Clicked;
    

    you have assigned the click event twice. remove one of these (preferably the code behind)

Answers

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited May 2016 Accepted Answer
    <Button Clicked="buttonSave_Clicked"/>
    buttonSave.Clicked += buttonSave_Clicked;
    

    you have assigned the click event twice. remove one of these (preferably the code behind)

  • andradedearthurandradedearthur USMember

    Thank you @JulienRosen . Fixed it and it's working great. What a stupid mistake, haha. In my defense it's my first experience with XAML, but still...

Sign In or Register to comment.