在Xamarin Forms C#上设置后退按钮标题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Xamarin Forms C#上设置后退按钮标题相关的知识,希望对你有一定的参考价值。
我对c#和Xamarin很新,我刚遇到一个问题。我似乎无法在导航页面上设置后退按钮标题。我尝试过使用静态SetBackButtonTitle方法,但它似乎没有将后退按钮设置为我想要的标题'Test',而是它给我'默认'标题,即“返回”。
// The root page of your application
var content = new ContentPage();
var CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
var NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
layout.Children.Add(NextPage);
content.Content = layout;
var content2 = new ContentPage();
var navigation = new NavigationPage(content);
NavigationPage.SetHasBackButton(navigation,true);
NavigationPage.SetBackButtonTitle(navigation,"Test");
NextPage.Clicked += NextPageClicked;
async void NextPageClicked(object sender, EventArgs e)
{
await navigation.PushAsync(content2);
}
MainPage = navigation;
}
答案
如果要在第二页中更改后退按钮的标题(在导航栏的左侧),则应将第一页作为参数调用方法SetBackButtonTitle。所以请将NavigationPage.SetBackButtonTitle(navigation,"Test");
修改为NavigationPage.SetBackButtonTitle(content,"Test");
以上是关于在Xamarin Forms C#上设置后退按钮标题的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms ViewRenderer音频不会停留在主页/后退按钮上
在 Xamarin.Forms UWP 应用程序中拦截鼠标后退按钮
更改 Xamarin Forms Android 导航页面颜色(按钮和返回)