簡體   English   中英

Xamarin形成iOS上未顯示的MasterDetailPage菜單圖標

[英]Xamarin forms MasterDetailPage Menu icon not shown on iOS

我有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非常重要,反之亦然。

解決方案是設置用於MasterPageIcon屬性。

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

這假設您的iOS項目中有一個名為menuIcon.png的Resources文件夾下的menuIcon.png ,它是您想要的漢堡包圖標。 您還需要在Detail周圍使用NavigationPage ,因為Icon顯示在導航欄區域中。

Unicode字符可用於顯示“漢堡包”菜單圖標。

您可以指定Title="☰" 對於主頁面ContentPage -top級別標記。

如果使用圖標,則可以繪制比char更好的圖標。 但是如果你可以接受的話,使用Unicode char很簡單。

它適用於iOS,不會破壞Android。

鏈接:

如果您將母版頁包裝在導航頁面中,請將該圖標放在導航頁面上。

這對我有用:

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" };
    }
}

如果它可以幫助某人你可以在設置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的奇怪錯誤,我無法直接從MasterPage 構造函數設置值。

我認為你必須在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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM