我的代碼如下。我不知道命令是否正確執行ToolbarItem。編譯沒有錯誤。當點擊了baritem時,什麼也沒有發生。ToolBar中的Toolbaritem不點擊
--- Xaml
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local ="clr-namespace:SembIWIS.View"
BackgroundColor="White"
Title="Repair and Service"
x:Class="MyMainMenu">
<ContentPage.ToolbarItems>
<ToolbarItem Name="MenuItem1" Order="Primary" Icon="itemIcon1" Command="{Binding Item1Command}" Priority="0" />
<ToolbarItem Name="MenuItem2" Order="Primary" Icon="itemIcon2" Priority="1" />
</ContentPage.ToolbarItems>
<local:Product>
</local:Product>
<local:Service>
</local:Service>
</TabbedPage>
--------- Code Behind:
public partial class MyMainMenu : TabbedPage
{
public ICommand Item1Command { get; private set; }
public MyMainMenu()
{
InitializeComponent();
BindingContext = this;
NavigationPage.SetHasBackButton(this, true);
Init();
}
private void Init()
{
this.Item1Command = new Command((sender) =>
{
Navigation.PushAsync(new UpdateProduct());
});
}
你還沒有爲你的視圖設置綁定上下文,所以'Command =「{Binding Item1Command}」'沒有綁定任何東西。一個短期的解決方法是在'MyMainMenu'構造函數中設置綁定上下文:'BindingContext = this;',儘管您可能想要調查MVVM模式並將您的ViewModel移動到一個對UI不瞭解的單獨的類。 – Damian
這是在同一頁面上完成的。你能否向我展示如何以及在何處添加綁定上下文。 – MilkBottle
我更新了我的評論以解釋(我過早地輸入)。 – Damian