如何在Xamarin.Forms的MasterDetailPage中自定义箭头图标、页面图标和页面标题

10

我在Visual Studio 2015中创建了一个新的空白应用(Xamarin.Forms Portable)项目,并修改了App.cs以获取“汉堡菜单”:

public class App : Application
{
    public App()
    {
        var masterPage = new ContentPage()
        {
            Content = new Label { Text = "Hello from Master!"},
            Title = "Master Page"
        };

        var detailPage = new ContentPage()
        {
            Content = new Label { Text = "Hello from Detail!" },
            Title = "Detail Page"
        };

        var mainPage = new MasterDetailPage()
        {
            Master = masterPage,
            Detail = detailPage,
            Title = "Main Page"
        };

        // The root page of your application
        MainPage = mainPage;
    }
    . . .
}

一切都运作良好,但我如何自定义这四个东西:

1) 隐藏/更改箭头

2) 隐藏/更改图标

3) 隐藏/更改标题文本

4) 隐藏整个工具栏

MasterDetailPage

1个回答

12
  1. You can change arrow to hamburger icon if you use your DetailPage within NavigationPage:

    Detail = new NavigationPage(detailPage);
    
  2. To change icon, just change project files:

    • YourProject/Resources/drawable/icon.png
    • YourProject/Resources/drawable-hdpi/icon.png
    • YourProject/Resources/drawable-xhdpi/icon.png
    • YourProject/Resources/drawable-xxhdpi/icon.png

    or on your MasterDetailPage set Icon property to another resource.

    If you want to hide icon - it only applies to Android. It can be solved with custom renderer (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/):

    public class CustomNavigationRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged (e);
    
            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon (Resource.Color.transparent);
        }
    }
    

    EDIT: It can also be done in MainActivity.cs:

    ActionBar.SetIcon (new ColorDrawable(Resources.GetColor (Android.Resource.Color.Transparent)));
    
  3. Just use Title property on Page.

  4. SetHasNavigationBar(page, false);


但是我在便携式项目中没有找到资源文件夹,但在xamarinMyProject.Droid中可用。 - Ashish-BeJovial
我使用了你的代码,actionBar 得到了空值。我做错了什么? - Joseph Katzman
@JosephKatzman 如果你的MainActivity是从FormsAppCompatActivity扩展而来的,那么你需要使用SupportActionBar而不是ActionBar。 - VahidShir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接