2016-10-15 41 views
3

我剛從默認的(app.cs)模板創建了一個可移植的應用程序,沒有更改任何代碼(儘管看起來我必須更新Xamarin.Forms來修復啓動異常)並運行它。我得到這個(無圖標顯示):c#Xamarin Forms:NavigationPage圖標不顯示

enter image description here

只是要徹底,這裏是代碼(再次,沒有從模板更改):

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的書(第24章源可以發現here)運行ModelessAndModel應用程序,我得到這樣的(圖標舒G):

enter image description here

在沒有任何特殊代碼。 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 
       } 
      }; 
     } 
    } 
} 

機器人項目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()); 
    } 
} 

我敢肯定,這是簡單的東西,但我不能似乎找到了關鍵的區別。 需要什麼才能讓我的應用程序在導航窗格中顯示圖標?

+0

我覺得差異可能是Petzold的示例使用「FormsApplicationActivity」,而新模板使用「FormsAppCompatActivity」作爲「MainActivity」的基類。 – jgoldberger

回答

6

對於使用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> 

如果還有更好的方法來做到這一點,我還沒有找到它。

+0

經過測試,它的工作原理。我很樂意採用其他方式來做到這一點,但這種方式很有效,所以我對此很滿意 – Jahmic

+1

但這不是一回事。這爲所有頁面添加了它。如果您只想爲1頁添加圖標怎麼辦? – batmaci

+0

其實它是一回事。當使用'FormsAppCompatActivity'作爲Android'MainActivity'的基類時,圖標從不默認顯示,所以這個答案解決了這個問題。你所問的是一個不同的問題,所以請發佈一個新問題。 – jgoldberger

0

「應用程序:標識」 還與FormsAppCompatActivity:

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

但 「應用程序:navigationIcon」 仍然無法正常工作(Xamarin.Forms 2.3.4.270)

+0

這工作正常!但是,如果你這樣做,你將無法控制圖標的位置(例如居中水平)。此外,您需要添加以下行以使其正常工作: xmlns:app =「http://schemas.android.com/apk/res-auto」 – goemic