2
我想用Xamarin.Forms構建我的第一個簡單的應用程序。綁定ToolbarItem點擊Xamarin.Forms
在這個應用程序中,我有一個ListPage和一個工具欄(在一個NavigationPage中)。
工具欄有一個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");
});
}
}
//...
我已經試過:
- 你看到上面
- 創建工具欄和按鈕只有通過什麼C#(並添加一個
async() => { ... }
參數的構造函數ToolbarItem) - 定期醇」
(object sender, System.EventArgs e) => { ... }
事件偵聽器(通過代碼.Clicked +=
)
或者,你可以只設置this.BindingContext =這一點; – Jason
在我的測試中無法按預期工作。你試過了嗎? –