2016-03-26 8 views
2

私はXamarin.Formsで初めての簡単なアプリケーションを構築しようとしています。バインディングToolbarItem Xamarin.Formsでクリック

このアプリケーションでは、ListViewとツールバー(NavigationPage内)を持つContentPageがあります。

ツールバーには、クリックするとメソッドを実行するToolbarItemがあります。私はGoogleの薄いを検索したにもかかわらず、私は単にそれを働かせることはできません...

誰かが私が行方不明を教えてくれますか?

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> 

分離コード:私が試した

//... 
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. あなたは
  2. ツールバーボタンのみスルーを作成する上で見る何C#(そして012を追加するパラメータ
  3. 定期老い.Clicked +=とコードを通じて(object sender, System.EventArgs e) => { ... }イベントリスナー()

答えて

0

)ToolbarItemコンストラクタに私はそれがバインディングコンテキストの問題だと思います。あなたが別のクラス(理想的にViewModelに)にコマンドを持っているし、あなたのページのために、このような結合のコンテキストを使用している場合、それはあなただけでthis.BindingContextを設定することができ、

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

代わりに期待どおりに動作しなければならない=この; – Jason

+0

私のテストで期待どおりに動作しません。試しましたか? –

関連する問題