我刚刚从默认的(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);
}
安卓项目,主活动:
[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章,源代码可以在这里找到)时,我会得到这个(显示图标!):
代码中没有什么特别的。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
}
};
}
}
}
[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());
}
}
我相信这是一件简单的事情,但我似乎找不到关键的区别。如何让我的应用程序在导航窗格中显示图标?
FormsApplicationActivity
,而新模板将FormsAppCompatActivity
作为MainActivity
的基类。 - jgoldberger - MSFT