首页 文章

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

提问于
浏览
0

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

enter image description here

1 回答

  • 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();
        }
    }
    

相关问题