Xamarin.Forms Action Bar - 中心对齐图像

时间:2016-08-24 17:08:00

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

使用Xamarin.Forms,如何获得与下图所示应用程序相同的效果,特别是在操作栏/页面工具栏上显示居中图像(蓝色框中的部分)? 我希望在该部分中有一个长宽的图像,该解决方案必须适用于Android,iOS,Windows Phone和Universal Windows(即使它意味着编写自定义渲染器或平台特定的xamarin代码)。

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议您创建自己的Xamarin.Forms视图并自行处理类似于此的导航:

public class CustomBackNavigationBar : StackLayout
{
    public Image BackIcon;
    public Image Icon;
    public Label IconTitle;
    public StackLayout IconContainer;

    public CustomBackNavigationBar(string title, string icon)
    {
        Padding = new Thickness(15,5);
        HeightRequest = 40;
        Orientation = StackOrientation.Horizontal;
        VerticalOptions = LayoutOptions.Start;
        BackgroundColor = StaticData.BlueColor;
        Spacing = 15;

        BackIcon = new Image
        {
            Source = StaticData.BackIcon,
            HorizontalOptions = LayoutOptions.Start
        };

        Label Title = new Label
        {
            Text = title,
            TextColor = Color.White,
            FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label)),
            FontAttributes = FontAttributes.Bold,
            VerticalTextAlignment = TextAlignment.Center
        };

        Icon = new Image
        {
            Source = icon
        };

        IconTitle = new Label
        {
            Text = StaticData.CallAgent,
            TextColor = Color.White,
            FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)),
        };

        IconContainer = new StackLayout
        {
            HorizontalOptions = LayoutOptions.EndAndExpand,
            Spacing = 2,
            Children = { Icon, IconTitle }
        };

        Children.Add(BackIcon);
        Children.Add(Title);
        Children.Add(IconContainer);

        #region Events

        BackIcon.GestureRecognizers.Clear();
        BackIcon.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = new Command(PopAsync)
        });

        #endregion

    }

    async void PopAsync()
    {
        await App.AppNavigation.PopAsync();
    }
}