我刚从默认(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章Source可以找到here),我得到了这个(Icon显示!):
代码没什么特别之处。 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
}
};
}
}
}
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());
}
}
我确定,这很简单,但我似乎无法找到关键的差异。 让我的应用在导航窗格中显示图标需要什么?
答案 0 :(得分:6)
对于使用FormsAppCompatActivity
的表单应用,我认为向工具栏添加图标的方法是在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>
如果有更好的方法,我还没有找到它。
答案 1 :(得分:0)
&#34;应用程序:标志&#34;也适用于FormsAppCompatActivity:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
...
app:logo="@drawable/icon" />
但&#34; app:navigationIcon&#34;仍然没有工作(Xamarin.Forms 2.3.4.270)