Xamarin将图像添加到导航栏

时间:2019-11-20 21:07:55

标签: xamarin xamarin.forms xamarin.android xamarin.ios

我有问题。我用Shell创建了一个Master Detail Page。但是现在我想将图像添加到导航栏中,所以要清楚一点:图像 ... 不是图标

这是我的代码:

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:MyApp"
       x:Class="MyApp.SideMenuItems" BackgroundColor="#212121"
       FlyoutBackgroundColor="#212121">

    <Shell.FlyoutHeader>
        <local:SideMenuHeader />
    </Shell.FlyoutHeader>

    <Shell.ItemTemplate>
        <DataTemplate>
            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal" Padding="30, 15, 0, 15">
                <Image Source="{Binding Icon}" HeightRequest="35" />
                <Label Text="{Binding Title}" TextColor="White" FontSize="Large" VerticalOptions="Center" HorizontalOptions="Start" />
            </StackLayout>
        </DataTemplate>
    </Shell.ItemTemplate>

    <FlyoutItem Title="SideNav"
                Shell.TabBarIsVisible="False"
                FlyoutDisplayOptions="AsMultipleItems">
        <ShellContent Title="Home" Icon="Home_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}"/>
        <ShellContent Title="Search" Icon="Search_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
        <ShellContent Title="Messages" Icon="Chats_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
        <ShellContent Title="Favorites" Icon="Favorites_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
        <ShellContent Title="Settings" Icon="Settings_Dark.png" IsTabStop="true" ContentTemplate="{DataTemplate local:HomePage}" />
    </FlyoutItem>
</Shell>

如何在中间的顶部导航栏中添加图像?

我尝试过:

<Shell.TitleView>
    <Image Source="Title_Dark.png" HeightRequest="30" VerticalOptions="CenterAndExpand" />
</Shell.TitleView>

但是屏幕上没有图像吗?

3 个答案:

答案 0 :(得分:0)

我还没有测试过,但是您应该可以在带有Shell的XAML中使用TitleView,因此可以在其中添加它。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical#displaying-views-in-the-navigation-bar

答案 1 :(得分:0)

如果在外壳的导航栏中显示视图,则将以下代码添加到主页ComtentPage。

<ContentPage ...>
<Shell.TitleView>
    <Image Source="xamarin_logo.png"
           HorizontalOptions="Center"
           VerticalOptions="Center" />
</Shell.TitleView>
...

Shell类定义了View类型的TitleView附加属性,该属性使所有Xamarin.Forms视图都可以显示在导航栏中。

虽然可以在子类化的Shell对象上设置此属性,但是也可以在想要在导航栏中显示视图的任何页面上设置此属性。

有关更多详细信息,您可以看一下:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/configuration

更新: 如果要更改导航栏的高度大小,可以在您的Android项目中的style.xml文件中使用高度来调用 android:actionBarSize

<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from https://aka.ms/material-colors -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>



<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
<item name="android:actionBarSize">250dp</item>
</style>



<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>

enter image description here

如果我的答复对您有帮助,请记住,谢谢。谢谢。

答案 2 :(得分:0)

通常,我将导航栏显示设置为false,然后将自己的导航栏视图创建为contentview。因此,您可以根据需要自定义它