【问题标题】:Xamarin forms MasterDetailPage Menu icon not shown on iOSXamarin 窗体 MasterDetailPage 菜单图标未在 iOS 上显示
【发布时间】:2017-09-05 16:51:07
【问题描述】:

我有 Xamarin.Forms 项目。 我在 NavigationPage 中有 MasterDetailPage。 我设置了 MasterDetailPage 的图标属性,以便应该将图标设置为导航栏的左上角。但它不起作用。

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        var masterDetailpage = new MasterDetailPage {
            Icon = "menuIcon.png",
            Master = new Page { Title = "Sample"},
            Detail = new Page()
        };

        MainPage = new NavigationPage(masterDetailpage);
    }
}

这永远行不通。如果我将 NavigationPage 作为 MasterDetailPage 的 Detail 属性,并在 Master 上设置图标。有用。 但是在 NavigationPage 中包含 MasterDetailPage 非常重要,反之亦然。

【问题讨论】:

  • 我可能错了。但是 Xamarin Docs 仅在主详细信息页面中显示 Android 版本的页面图标,还是您的意思是别的? developer.xamarin.com/guides/xamarin-forms/…
  • 默认情况下,iOS 上的图标是不显示的,但是您通常可以在 iOS 上添加图标并将其添加为页面的图标属性。它适用于某些情况,但不适用于我的情况。
  • 导航页面是否会以某种方式覆盖此属性设置?
  • 我不知道。在文档中解释了导航页面应该设置为MasterDetailPage的详细页面。但我不想要那个
  • 你设置详情页吗?

标签: ios xaml xamarin xamarin.ios xamarin.forms


【解决方案1】:

解决方案是将PageIcon 属性设置为Master

 var masterDetailpage = new MasterDetailPage {            
        Master = new Page { Title = "Sample", Icon = "menuIcon.png"},
        Detail = new NavigationPage(new Page())
    };

这假设您的 iOS 项目中有一个名为 menuIcon.png 的资源文件夹下的 png,这是您想要的汉堡图标。您还需要在Detail 周围使用NavigationPage,因为Icon 显示在导航栏区域中。

【讨论】:

  • 谢谢,但如果你最后读到我的问题,我会写你写的。但是这种结构的工作方式不同,这就是为什么我想在 NavigationPage 中有 MasterDetailPage 而不是你提到的方式
【解决方案2】:

Unicode 字符可用于显示“汉堡”菜单图标。

您可以为母版页ContentPage-顶级标签指定Title="☰"

如果你使用一个图标,你可能会画出比char更好的图标。但是,如果您可以接受,使用 Unicode char 就足够简单了。

它适用于 iOS,不会破坏 Android。

链接:

【讨论】:

    【解决方案3】:

    如果您将母版页包装在导航页中,请将图标放在导航页上。

    这对我有用:

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
    
            var masterDetailpage = new MasterDetailPage {
                Icon = "menuIcon.png",
                Master = new Page { Title = "Sample"},
                Detail = new Page()
            };
    
            MainPage = new NavigationPage(masterDetailpage) { Title = "Sample", Icon = "menuIcon.png" };
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果它对某人有帮助,您可以在设置 MainPage 之前设置菜单的标题,就像我在下面的示例代码中所做的那样:

      var master = new MasterPage();
      master.Master.Icon = "my_icon.png";
      master.Master.Title = AppResources.Menu; // To get the value from resource files
      MainPage = master;
      

      MasterPage 继承自 MasterDetailsPage 并且由于 Xamarin 的奇怪错误,我无法直接从 MasterPageconstructor 设置值。

      【讨论】:

        【解决方案5】:

        我认为您必须在您的 AddDelegate 中添加一个常见的修复方法

        namespace myApp.iOS
        {
            [Register("AppDelegate")]
            public partial class AppDelegate : 
                                 global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
            {
                UIWindow window;
        
                public override bool FinishedLaunching(UIApplication app, NSDictionary options)
                {
                    global::Xamarin.Forms.Forms.Init();
        
                    // fix for iOS
                    window = new UIWindow(UIScreen.MainScreen.Bounds);
                    window.RootViewController = App.GetMainPage().CreateViewController();
                    window.MakeKeyAndVisible();
        
                    LoadApplication(new App());
        
                    return base.FinishedLaunching(app, options);
                }
            }
        }
        

        App.xaml.cs 的 PCL 项目中,您必须添加

        public App()
        {
            // The root page of your application
            MainPage = GetMainPage();
        }
        
        public static Page GetMainPage()
        {
            return new RootPage();
        }
        

        【讨论】:

        • 不幸的是,这具有误导性,并不能解决问题。您提出的代码对于 LoadApplication 正在执行的操作和 base.FinishedLaunching 调用是多余的。
        猜你喜欢
        • 1970-01-01
        • 2017-08-14
        • 2018-01-20
        • 2017-07-26
        • 1970-01-01
        • 2018-08-16
        • 2018-12-24
        • 1970-01-01
        相关资源
        最近更新 更多