我刚刚从默认 (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 章来源可以找到here https://github.com/xamarin/xamarin-forms-book-samples),我明白了(图标显示!):
代码中没有什么特别的。应用程序.cs:
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new 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());
}
}
我确信,这很简单,但我似乎找不到关键的区别是什么。怎样才能让我的应用程序的图标显示在导航窗格中?