Issue
I have a problem. I am using the following code: https://github.com/Polarts/CrossFAB
In page1.xaml
I added the FabMenu like 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 x:Name="btnAddItem" BGColor="#59E1FF" IconSrc="add_item_icon" BindingContext="{x:Reference ItemPage}" OnClickCommand="{Binding btnAddItemCommand}" />
</c:FloatingMenu>
And in page1.xaml.cs I added the command like this:
public partial class Page1 : ContentPage
{
public static ICommand btnAddItemCommand { get; set; }
public Page1 ()
{
InitializeComponent();
btnAddItemCommand = new Command(OpenNewItemPage);
}
private async void OpenNewItemPage()
{
await Application.Current.MainPage.Navigation.PushAsync(new NewItemPage());
}
}
The problem is that when I click the button, it gives an error in FloatingButton.xaml.cs
on the line: OnClickCommand.Execute(null);
.
The error is:
Object reference not set to an instance of an object
This is because according to my code OnClickCommand
is not set to an instance, so has value null.
The code of FloatingButton.xaml.cs can be found here: https://github.com/Polarts/CrossFAB/blob/master/CrossFAB/Controls/FloatingButton.xaml.cs
How can I fix this?
Solution
I think you does not bind the command successfully.
In .cs, set the BindingContext = this;
and do not define the btnAddItemCommand
as a static property:
public partial class MainPage : ContentPage
{
public ICommand btnAddItemCommand { get; set; }
public MainPage()
{
InitializeComponent();
btnAddItemCommand = new Command(OpenNewItemPage);
BindingContext = this;
}
private void OpenNewItemPage()
{
Console.WriteLine("OpenNewItemPage");
}
}
In xaml, remove the bindingContext part:
<StackLayout>
<!-- Place new controls here -->
<Button x:Name="btnAddItem" Text="test" Command="{Binding btnAddItemCommand}" />
</StackLayout>
Answered By - nevermore
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.