在Xamarin.Android中删除导航后退箭头

时间:2019-03-04 14:06:16

标签: xamarin xamarin.forms xamarin.android

我有一个Xamarin.Forms应用程序。我想删除/隐藏导航栏中的后退箭头,但保留标题。我可以在iOS中使用NavigationPageRenderer中的以下代码来做到这一点:

UINavigationBar.Appearance.BackIndicatorImage = new UIImage();
UINavigationBar.Appearance.BackIndicatorTransitionMaskImage = new UIImage();

在Android中是否可以在渲染器或MainActivity中使用与此等效的代码?我在this.ActionBar.SetDisplayHomeAsUpEnabled(false);内尝试过MainActivity,但是ActionBar始终返回null。以下是我的MainActivity代码:

public class MainActivity : FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
       TabLayoutResource = Resource.Layout.Tabbar;
       ToolbarResource = Resource.Layout.Toolbar;

       base.OnCreate(bundle);

       global::Xamarin.Forms.Forms.Init(this, bundle);
       LoadApplication(new App());
       if (Window != null)
       {
          Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
       }

       this.ActionBar.SetDisplayHomeAsUpEnabled(false);

     }
}

我希望导航栏看起来像下面的图片(在我的iOS应用中)。

enter image description here

后退箭头只是后退按钮的标题:NavigationPage.SetBackButtonTitle(this, "\u25C3");

在我的ContentPage中:

public partial class HomeTabPage : ContentPage
{
   public HomeTabViewModel vm;

   public HomeTabPage()
   {
      InitializeComponent();
      BindingContext = vm = new HomeTabViewModel(this);
      NavigationPage.SetHasBackButton(this, false);
      NavigationPage.SetBackButtonTitle(this, "\u25C3");
   }
}

1 个答案:

答案 0 :(得分:1)

解决方案: 您可以将自定义视图定义为特定平台(Android)上的内容的navigationBar。请参考以下代码。

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();

        if(Device.RuntimePlatform=="Android")
        {
            NavigationPage.SetHasBackButton(this, false);
            NavigationPage.SetTitleView(this, SetBackView("Title", "back"));
        }


    }

    private void BackButton_Clicked(object sender, EventArgs e)
    {
        Navigation.PopAsync();
    }

    StackLayout SetBackView (string title,string backButtonContent)
    {
        Button backButton = new Button()
        {

            Text = backButtonContent,
            TextColor = Color.White,
            FontAttributes=FontAttributes.None,
            BackgroundColor = Color.Transparent,
            Margin = new Thickness(-20,0,0,0),
        };
        backButton.Clicked += BackButton_Clicked;

        StackLayout stackLayout = new StackLayout
        {

            Children = {

                backButton,

                new Label{

                    HorizontalTextAlignment=TextAlignment.Center,
                    VerticalTextAlignment=TextAlignment.Center,
                    Text=title,
                    TextColor=Color.White,
                    BackgroundColor=Color.Transparent,
                },

            },
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Orientation = StackOrientation.Horizontal,
        };

        return stackLayout;
    }

}

效果如下所示,您可以根据需要设置页面标题和backButton的内容。

enter image description here

相关问题