我正在嘗試在我的移動應用程式中創建自定義導航/“回傳”按鈕。我想將命令系結為自定義樣式中的設定器屬性,然后將該樣式應用于名為 HeaderNavigation 的更大自定義元素中的 ImageButton 元素。
一切都建立起來,這樣我就可以看到我的 ImageButton 應用了它的樣式。當我查看它的樣式時,我還可以看到應用于 ImageButton 的命令,但是,如果我單擊 ImageButton,則不會呼叫該命令。
我已經玩了一點系結,因為我假設這是問題,但還沒有骰子,所以我擔心我正在嘗試的東西是不可能的,或者我可能缺少一些愚蠢的東西。
即使有人可以給我一些移動系結的除錯技巧,那也會很有幫助。
謝謝!
下面的代碼:
應用程式.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>
應用程式.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);
}
}
}
uj5u.com熱心網友回復:
我的理解是,您的目標是讓自定義中的后退按鈕NavigationHeader呼叫OnBackButtonPressed您App類中的方法:
public partial class App : Application
{
public async void OnBackButtonPressed(object sender, EventArgs e)
{
// This pop up will be the test.
await MainPage.DisplayAlert("Message from button", "It worked", "OK");
}
}
我已經測驗過的一種方法
uj5u.com熱心網友回復:
兩個問題:
- 該方法不是命令。屬性
Command需要系結到一個Command. 在 App.xaml.cs 中:
public Command BackButtonCommand { get; } = new Command(BackButtonAction);
private void BackButtonAction()
{
...
}
App沒有BindingContext. 請參閱 App 物件本身的命令。將 xaml 值更改為:
... Value="{Binding Source={RelativeSource AncestorType={x:Type local:App}}, Path=BackButtonCommand}" ...
注意:未經測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487695.html
