NavigationPage图标未显示

NavigationPage图标未显示

本文介绍了C#Xamarin形式:NavigationPage图标未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从默认(app.cs)模板创建了一个可移植的应用程序,未更改任何代码(尽管似乎我必须更新Xamarin.Forms以修复启动异常)并运行它.我知道了(没有图标显示):

I have just created a portable app from the default (app.cs) template, changed none of the code (although it seems I had to update Xamarin.Forms to fix a startup exception) and ran it. I got this (No icon showing):

仅此而已,以下是代码(再次,模板中未做任何更改):

Just to be thorough, here is the code (once again, nothing changed from the template):

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项目,主要活动:

Droid project, main activity:

[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章可以找到这里),我得到了(图标显示!):

However, when I run the ModelessAndModel app from Petzold's book (chapter 24 Source can be found here), I get this (Icon showing!):

代码没什么特别的. app.cs:

Nothing special in code. app.cs:

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

MainPage.cs:

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:

droid project 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());
    }
}

我敢肯定,这很简单,但是我似乎找不到关键的区别. 要使我的应用在导航窗格中显示图标是什么?

I'm sure, it is something simple, but I can't seem to find what the key diffrence is. What does it take to have my app have the icon show up in the navigation pane?

推荐答案

对于使用FormsAppCompatActivity的Forms应用程序,我相信向工具栏添加图标的方法是在工具栏的.axml文件中完成此操作.在Android应用程序项目中.新模板Forms应用程序中工具栏的默认布局为:

For a Forms app using FormsAppCompatActivity, I believe that the way to add an icon to the toolbar is to do it in the .axml file for the Toolbar in the Android app project. The default layout for the toolbar in a new template Forms app is:

<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" />

但是您可以为此添加控件,例如添加图标:

But you can add controls to this, e.g. to add an icon:

<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>

如果有更好的方法可以做到这一点,我还没有找到它.

If there is a better way to do this, I have yet to find it.

这篇关于C#Xamarin形式:NavigationPage图标未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:42