【问题标题】:Removing navigation back arrow in Xamarin.Android在 Xamarin.Android 中删除导航返回箭头
【发布时间】:2019-07-25 20:44:50
【问题描述】:

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

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

在我的渲染器或 MainActivity 中,我可以在 Android 中使用任何等效的代码吗?我在MainActivity 中尝试了this.ActionBar.SetDisplayHomeAsUpEnabled(false);,但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 应用中)。

后退箭头就是后退按钮标题: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");
   }
}

【问题讨论】:

  • 这类似于Page.HasBackButton = false。你可能必须投这个:ViewController.ParentViewController.NavigationItem.SetHidesBackButton(!((CustomContentPage)this.Element).HasBackButton, false);
  • 如果你的意思是NavigationPage.SetHasBackButton(this, false); 那么是的,我试过了,但没有用
  • 调用 NavigationPage.SetHasBackButton(this, false) 时使用了哪个页面?如果你在 NavigationPage 上设置它,它不会做任何事情。
  • 你能提供你想要的效果截图吗? NavigationPage.SetHasBackButton(this, false);如果你在 contentPage 中调用它应该可以工作。
  • 我确实在我的 ContentPage 中调用了它

标签: xamarin xamarin.forms xamarin.android


【解决方案1】:

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

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的内容可以随意设置。

【讨论】:

  • 谢谢。我会试试这个,稍后会回复你。
  • 在一个测试项目上尝试过,它似乎工作。谢谢!
  • 快乐编码:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 2016-05-05
  • 2022-01-15
  • 1970-01-01
  • 2013-02-20
相关资源
最近更新 更多