2016-03-26 32 views
2

Xamarin.Forms ile ilk, basit uygulamayı oluşturmaya çalışıyorum.Binding ToolbarItem Tıklayın Xamarin.Forms

Bu uygulamada bir ListView ve bir Araç Çubuğu (bir NavigationPage içinde) içeren bir ContentPage'im var.

Araç Çubuğunda, tıklatıldığında bir yöntem çalıştırması gereken bir Araç Çubuğu vardır. Google’ı ince aramış olmama rağmen, işte çalışamıyorum ...

Neyi kaçırdığımı kimse anlatabilir mi?

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: denedim

//... 
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"); 
       }); 

     } 
    } 
//... 

:

  1. Eğer
  2. araç çubuğunu ve düğmesi yalnızca aracılığıyla oluşturma yukarıya bakınız neler C# (Ve bir 012 ekleyerekparametre
  3. Düzenli ol' .Clicked += ile kod üzerinden (object sender, System.EventArgs e) => { ... } olay dinleyicisi()

cevap

0

) ToolbarItem kurucusuna bunun bağlayıcı bir bağlam sorunu olduğunu düşünüyorum. Eğer ayrı sınıfa (ideal bir ViewModel) içine komutu varsa ve sayfanız için bu kadar bağlayıcı içeriği kullanırsanız, o zaman sadece this.BindingContext ayarlayabilirsiniz,

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

    public ICommand CreateCommand { get; private set; } 
} 

... 

public SetsPage() { 
     var vm = new MyVm(); 
     this.BindingContext = vm; 

     InitializeComponent(); 
... 
+0

alternatif beklendiği gibi çalışmalıdır = Bu; Testimde beklendiği gibi çalışmayan – Jason

+0

. Denedin mi? –