我的代码如下。我不知道命令是否正确执行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