Why is the second toolbaritem not showing

AustyAusty Member ✭✭✭

l have the following code in my master page but the second tool bar item is not displaying.

<ContentPage.ToolbarItems>


</ContentPage.ToolbarItems>

Only one logo is been displayed. Can anyone help please

Best Answers

Answers

  • AustyAusty Member ✭✭✭

    Thanks @yelinzh .. Apparently the width of one of the icon was too large. l reduced it and it worked fine...l have a challenge with centering the logo though..can you help

  • AustyAusty Member ✭✭✭

    @yelinzh Thanks.it worked perfectly..l have another question if you can help... B) ..l am trying to apply a gradient background to a stacklayout..How would you create a linear gradient on stacklayout

  • JarvanJarvan Member, Xamarin Team Xamurai

    @Austy
    You need to create a StackLayout custom renderer to achieve the feature.
    page.xaml

    <local:CustomLayout StartColor="Green" EndColor="Red">
        ...
    </local:CustomLayout>
    

    CustomLayout.cs

    public class CustomLayout : StackLayout
    {
        public Color StartColor { get; set; }
        public Color EndColor { get; set; }
    }
    

    CustomLayoutRenderer.cs

    [assembly:ExportRenderer(typeof(CustomLayout),typeof(CustomLayoutRenderer))]
    namespace App19.Droid
    {
    #pragma warning disable CS0618 // Type or member is obsolete
        public class CustomLayoutRenderer : VisualElementRenderer<StackLayout>
        {
            private Color StartColor { get; set; }
            private Color EndColor { get; set; }
    
            protected override void DispatchDraw(global::Android.Graphics.Canvas canvas)
            {
    
                var gradient = new Android.Graphics.LinearGradient(0, 0, Width, 0,
    
                       this.StartColor.ToAndroid(),
                       this.EndColor.ToAndroid(),
                       Android.Graphics.Shader.TileMode.Mirror);
    
                var paint = new Android.Graphics.Paint()
                {
                    Dither = true,
                };
                paint.SetShader(gradient);
                canvas.DrawPaint(paint);
                base.DispatchDraw(canvas);
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
            {
                base.OnElementChanged(e);
    
                if (e.OldElement != null || Element == null)
                {
                    return;
                }
                try
                {
                    var stack = e.NewElement as CustomLayout;
                    this.StartColor = stack.StartColor;
                    this.EndColor = stack.EndColor;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message);
                }
            }
        }
    #pragma warning restore CS0618 // Type or member is obsolete
    }
    

    Refer to:
    https://stackoverflow.com/questions/28848545/add-gradient-background-to-layouts-in-xamarin-forms-visual-studio

  • AustyAusty Member ✭✭✭

    @yelinzh l am trying to implement this in ios but l get the following error

    Cannot convert type "Xamarin.forms.Layout" to comics.colors ..

    Find my code in IOS AppDelegate.cs below

    public class GradientColorRender : VisualElementRenderer<StackLayout>
    {
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
            Colors stack = (Colors)this.Element;
            CGColor startColor = stack.StartColor.ToCGColor();
            CGColor endColor = stack.EndColor.ToCGColor();
            #region for Vertical Gradient  
            //var gradientLayer = new CAGradientLayer();     
            #endregion
            #region for Horizontal Gradient  
            var gradientLayer = new CAGradientLayer()
            {
                StartPoint = new CGPoint(0, 0.5),
                EndPoint = new CGPoint(1, 0.5)
            };
            #endregion
            gradientLayer.Frame = rect;
            gradientLayer.Colors = new CGColor[] {
                startColor,
                endColor
            };
            NativeView.Layer.InsertSublayer(gradientLayer, 0);
        }
    
    
    }
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    Sorry, I konw little about IOS.

  • AustyAusty Member ✭✭✭

    @yelinzh l am getting the same error in while Implementing on Android..This section of the above code.

    var stack = e.NewElement as Colors;

    Is giving the same error as above.

  • JarvanJarvan Member, Xamarin Team Xamurai

    I shared my demo. You can check it.

  • AustyAusty Member ✭✭✭

    @yelinzh when l try to run your solution.. the packages cant be restored is the error l get.

  • AustyAusty Member ✭✭✭

    @yelinzh l still get the same error when l try to copy your code to my solution.

    Cannot convert type "Xamarin.forms.Layout" to comics.colors

    Thanks

Sign In or Register to comment.