Binding ToolbarItem Click in Xamarin.Forms

Viewed 9513

I'm trying to build my first, simple application with Xamarin.Forms.

In this app I have a ContentPage with a ListView and a Toolbar (inside a NavigationPage).

The Toolbar has a ToolbarItem that should run a method when clicked. Even though I've searched Google thin, I simply can't get it to work...

Can anybody tell me what I'm missing?

XAML:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:constants="clr-namespace:FlashCards;assembly=FlashCards"
x:Class="FlashCards.SetsPage" 
Title="Card Sets">
    <ContentPage.ToolbarItems>
          <ToolbarItem Name="Add" Icon="Icon-Button-Add.png" Command="{Binding CreateCommand}"></ToolbarItem>
    </ContentPage.ToolbarItems>
  <ListView x:Name="CardSetView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell Text="{Binding Title}" />
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

CodeBehind:

//...
public partial class SetsPage : ContentPage
    {
        ObservableCollection<CardSet> sets = new ObservableCollection<CardSet>();

        public Command CreateCommand { get; private set; }

        public SetsPage() {

            InitializeComponent();

            sets.Add(new CardSet{ Title = "Test 1" });
            sets.Add(new CardSet{ Title = "Test 2" });
            sets.Add(new CardSet{ Title = "Test 3" });

            CardSetView.ItemsSource = sets;

            this.CreateCommand = new Command(async (sender) =>
                {
                    Debug.WriteLine("Hello");
                });

        }
    }
//...

I've tried:

  1. What you see above
  2. Creating the toolbar and the button only through C# (And adding a async () => { ... } parameter to the ToolbarItem constructor)
  3. A regular ol' (object sender, System.EventArgs e) => { ... } event listener (through code with .Clicked +=)
1 Answers
Related