简体   繁体   English

Xamarin.Forms:如何设置 NavigationPage 栏的背景图片

[英]Xamarin.Forms: how to set background image of NavigationPage bar

I'm building a Xamarin.Forms app and I need to set the background of the nav bars to an image.我正在构建 Xamarin.Forms 应用程序,我需要将导航栏的背景设置为图像。 Is this possible without creating something super custom?如果不创建超级定制的东西,这可能吗? If not, what's the best way to go about it?如果没有,最好的方法是什么? Has anyone else done it already?有没有其他人已经这样做了?

Here's what I need to do.这是我需要做的。 Note the nav bar area with the back button, messaging icon, and "COMMENTARY" text.请注意带有后退按钮、消息图标和“评论”文本的导航栏区域。

在此处输入图像描述

Any ideas?有任何想法吗?

It's not possible with xamarin to change the navigation background image. xamarin 无法更改导航背景图像。 You have to do that in native for each platform.您必须针对每个平台以本机方式执行此操作。

  1. Android:安卓:

in Ressources/layout/Toolbar.axml remove background color and add:在 Ressources/layout/Toolbar.axml 中删除背景颜色并添加:

android:background="@drawable/yourImage"

2. iOS 2.iOS

in AppDelegate.cs add:在 AppDelegate.cs 添加:

UINavigationBar.Appearance.BarTintColor=UIColor.FromPatternImage(UIImage.FromFile("YOURIMAGE.png"));
// To change Text Colors to white here
UINavigationBar.Appearance.TintColor=UIColor.White;
// To change Title Text colors to white here
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White});
  1. Images图片

Don't forget to put your images in different ressources folders.不要忘记将图像放在不同的资源文件夹中。

Hope this help you !希望这对你有帮助!

You can add a background image and buttons to Navigation bar.您可以向导航栏添加背景图像和按钮。 element can be used to customise the element. element 可用于自定义元素。 You could use AbsoluteLayout to achieve what you want (button, Text...etc)您可以使用 AbsoluteLayout 来实现您想要的(按钮、文本等)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"                 
                 x:Class="MobileApp.Views.PhotoUploadPage">
   <NavigationPage.TitleView>
        <StackLayout>
            <Image Source="background_image.jpg"></Image>
        </StackLayout>
   </NavigationPage.TitleView>
   <ContentPage.Content>
           <StackLayout>
                <Label Text="Welcome to Multiple Photo Upload!"
                   VerticalOptions="CenterAndExpand" 
                   HorizontalOptions="CenterAndExpand" />
           </StackLayout>
   </ContentPage.Content>
</ContentPage>

https://forums.xamarin.com/discussion/139894/putting-an-image-on-navigation-bar https://forums.xamarin.com/discussion/139894/putting-an-image-on-navigation-bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM