我正在try 在我的移动应用程序中创建自定义导航/"后退"按钮.我想将该命令绑定为自定义样式中的setter属性,然后将该样式应用于称为HeaderNavigation的更大自定义元素中的ImageButton元素.
一切都是这样构建的,我可以看到我的ImageButton及其应用的样式.当我查看ImageButton的样式but时,我还可以看到应用于ImageButton的命令,如果单击ImageButton,则不会调用该命令.
我已经try 了一些绑定,因为我假设这就是问题所在,但还没有骰子,所以我担心我正在try 的是不可能的,或者可能我遗漏了一些愚蠢的东西.
即使有人能给我一些关于移动绑定的调试技巧,那也会很有帮助.
谢谢
代码如下:
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);
}
}
}