[英]Displaying the default back button of the navigation bar of a ContentPage assigned to the Xamarin.Forms Shell
[英]Xamarin.Forms: Handle Navigation toolbar button (Shell)
我想處理我的 Xamarin.Forms 應用程序(android 版本)中的任何返回點擊。
對於 Android/硬件按鈕非常簡單,我所要做的就是覆蓋 OnBackButtonPressed 方法。 但是這個頂部工具欄並不那么容易。 我聽說它應該與 Shell.SetBackButtonBehavior 一起使用,但由於某種原因,它對我不起作用。
我的代碼是這樣的,我把它放在了InitializeComponents()之后的頁面構造函數中。
Shell.SetBackButtonBehavior(this, new BackButtonBehavior {
Command = new Command(async () =>
await Shell.Current.DisplayAlert("Back","Back","Back")
)
});
知道有什么問題嗎?
該功能可以通過在xaml中定義Shell.BackButtonBehavior ,在cs中實現Command來實現。
這是我的一個小例子:
在xml中:
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding GoBack}" >
</BackButtonBehavior>
</Shell.BackButtonBehavior>
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
在 cs 中:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page2 : ContentPage
{
public ICommand GoBack { get; set; }
public Page2()
{
InitializeComponent();
GoBack = new Command(async () =>
await Shell.Current.DisplayAlert("Back", "Back", "Back"));
BindingContext = this;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.