[英]Xamarin Forms Command not found
我有个问题。 我正在使用以下 fabmenu: https : //github.com/Polarts/CrossFAB
现在我想将命令绑定到 fabmenu 按钮,因此我在 page1.xaml 中执行了以下操作:
<c:FloatingMenu Margin="0, 0, 10, 10" BGColor="#56D7A5" OpenIcon="openFab_icon" CloseIcon="closeFab_icon"
AbsoluteLayout.LayoutBounds=".95,.95" AbsoluteLayout.LayoutFlags="PositionProportional">
<c:FloatingButton x:Name="page1" BGColor="#59E1FF" IconSrc="page1" OnClickCommand="{Binding btnPage2Command}" />
</c:FloatingMenu>
在 page1.xaml.cs 中,我执行了以下操作:
public partial class page1 : ContentPage
{
public ICommand btnPage2Command{ get; set; }
public page1()
{
InitializeComponent();
btnPage2Command= new Command(OpenPage2);
}
private async void OpenPage2()
{
await Application.Current.MainPage.Navigation.PushAsync(new page2());
}
}
但我收到以下错误:
找不到“OnClickCommand”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配。
我现在已经添加了 BindingContext,但出现以下错误:
你调用的对象是空的
在 FloatingButton.xaml.cs 中的这一行(参见https://github.com/Polarts/CrossFAB ):
OnClickCommand.Execute(null);
我怎样才能解决这个问题?
您没有分配BindingContext
this.BindingContext = this;
你需要一个绑定上下文。 因此,您可以在构造函数或 xaml 文件中分配它。
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
....
x:Name="this">
你想用它
<c:FloatingMenu Margin="0, 0, 10, 10" BGColor="#56D7A5" OpenIcon="openFab_icon" CloseIcon="closeFab_icon"
AbsoluteLayout.LayoutBounds=".95,.95" AbsoluteLayout.LayoutFlags="PositionProportional">
<c:FloatingButton BindingContext="{x:Reference this}" x:Name="page1" BGColor="#59E1FF" IconSrc="page1" OnClickCommand="{Binding btnPage2Command}" />
</c:FloatingMenu>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.