I am trying to create a custom navigation/"back" button in my mobile app. I want to bind the command as a setter property in a custom style and then apply that style to the ImageButton element in a larger custom element called HeaderNavigation.
Everything builds so that I can see my ImageButton with its styles applied. I can also see the command applied to the ImageButton when I watch its styles but, if I click on the ImageButton, the command is not called.
I've played around with the binding a little bit because I'm assuming that's the issue but no dice yet so I'm worried what I'm attempting isn't possible or maybe there's something stupid I'm missing.
Even if someone could just give me some debugging tips for mobile bindings, that would be helpful.
Thanks!
Code below:
app.xaml
<Style x:Key="NavButton" TargetType="ImageButton" BasedOn="{StaticResource IconButton}">
<Setter Property="Source" Value="back.png"/>
<Setter Property="HorizontalOptions" Value="EndAndExpand"/>
<Setter Property="Command" Value="{Binding Source={RelativeSource AncestorType={x:Type local:App}},Path=BindingContext.OnBackButtonPressed}"/>
<Setter Property="BackgroundColor" Value="{StaticResource Background}"/>
</Style>
App.xaml.cs
public async void OnBackButtonPressed(object sender, EventArgs e)
{
// I do not get called :(
_ = await Navigation.PopAsync();
}
HeaderNavigation.cs
public class HeaderNavigation : StackLayout
{
private ImageButton _returnButton;
public HeaderNavigation()
{
_returnButton = new ImageButton();
_returnButton.Style = (Style)Application.Current.Resources["NavButton"];
Children.Add(_returnButton);
}
}
}
