我剛從默認的(app.cs)模板創建了一個可移植的應用程序,沒有更改任何代碼(儘管看起來我必須更新Xamarin.Forms來修復啓動異常)並運行它。我得到這個(無圖標顯示):c#Xamarin Forms:NavigationPage圖標不顯示
只是要徹底,這裏是代碼(再次,沒有從模板更改):
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):
在沒有任何特殊代碼。 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());
}
}
我敢肯定,這是簡單的東西,但我不能似乎找到了關鍵的區別。 需要什麼才能讓我的應用程序在導航窗格中顯示圖標?
我覺得差異可能是Petzold的示例使用「FormsApplicationActivity」,而新模板使用「FormsAppCompatActivity」作爲「MainActivity」的基類。 – jgoldberger