簡體   English   中英

c#Xamarin表單:未顯示NavigationPage圖標

[英]c# Xamarin Forms: NavigationPage icon not showing

我剛從默認(app.cs)模板創建了一個可移植的應用程序,沒有更改任何代碼(雖然我似乎必須更新Xamarin.Forms以修復啟動異常)並運行它。 我得到了這個(沒有圖標顯示):

在此輸入圖像描述

為了徹底,這里是代碼(再一次,模板沒有任何改變):

namespace App3
{
    public class App : Application
    {
        public App()
        {
            // The root page of your application
            var content = new ContentPage
            {
                Title = "App3",
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            HorizontalTextAlignment = TextAlignment.Center,
                            Text = "Welcome to Xamarin Forms!"
                        }
                    }
                }
            };

            MainPage = new NavigationPage(content);
        }

Droid項目,主要活動:

[Activity(Label = "App3", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.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());
    }
}

但是,當我從Petzold的書中運行ModelessAndModel應用程序時(第24章Source可以在這里找到),我得到了這個(Icon顯示!):

在此輸入圖像描述

代碼沒什么特別之處。 app.cs:

   public class App : Application
    {
        public App()
        {
            MainPage = new NavigationPage(new MainPage());
        }
...

MainPage.cs:

namespace ModelessAndModal
{
    public class MainPage : ContentPage
    {
        public MainPage()
        {
            Title = "Main Page";

            Button gotoModelessButton = new Button
            {
                Text = "Go to Modeless Page",
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
            gotoModelessButton.Clicked += async (sender, args) =>
            {
                await Navigation.PushAsync(new ModelessPage());
            };

            Button gotoModalButton = new Button
            {
                Text = "Go to Modal Page",
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
            gotoModalButton.Clicked += async (sender, args) =>
            {
                await Navigation.PushModalAsync(new ModalPage());
            };

            Content = new StackLayout
            {
                Children =
                {
                    gotoModelessButton,
                    gotoModalButton
                }
            };
        }
    }
}

droid項目MainActivity.cs:

[Activity(Label = "ModelessAndModal", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

我敢肯定,這很簡單,但我似乎無法找到關鍵的差異。 讓我的應用程序在導航窗格中顯示圖標需要什么?

對於使用FormsAppCompatActivity的Forms應用程序,我認為向工具欄添加圖標的方法是在Android應用程序項目中的工具欄的.axml文件中執行此操作。 新模板Forms應用程序中工具欄的默認布局是:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

但您可以為此添加控件,例如添加圖標:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
    <ImageView
        android:src="@drawable/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</android.support.v7.widget.Toolbar>

如果有更好的方法,我還沒有找到它。

“app:logo”也適用於FormsAppCompatActivity:

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
    ...
        app:logo="@drawable/icon" />

但“app:navigationIcon”仍然不起作用(Xamarin.Forms 2.3.4.270)

暫無
暫無

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

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