72157

Gradient Button in Xamarin Forms

Question:

<a href="https://i.stack.imgur.com/WH328.png" rel="nofollow"><img alt="Gradient Button" class="b-lazy" data-src="https://i.stack.imgur.com/WH328.png" data-original="https://i.stack.imgur.com/WH328.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

How do I create this button with Gradient Effect in Xamarin Forms using Renderer ?

Answer1:

In xamarin you can't add gradient colors as a built in feature. You have to create a different rendering feature. This <a href="https://baglabs.com/2017/07/14/creating-gradients-xamarin-forms/" rel="nofollow">link</a> will guide you.

Answer2:

In Xamarin Forms Craets

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

And then Renderer Android

public class GradientColorStackRenderer : VisualElementRenderer<StackLayout> { private Color StartColor { get; set; } private Color EndColor { get; set; } protected override void DispatchDraw(global::Android.Graphics.Canvas canvas) { #region for Horizontal Gradient var gradient = new Android.Graphics.LinearGradient(0, 0, Width, 0, #endregion 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<Frame> e) { base.OnElementChanged(e); if (e.OldElement != null || Element == null) { return; } try { var stack = e.NewElement as GradientColorStack; this.StartColor = stack.StartColor; this.EndColor = stack.EndColor; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message); } } }

And In Ios Project Rendering

public class GradientColorStackRenderer : VisualElementRenderer<Frame> { public override void Draw(CGRect rect) { base.Draw(rect); GradientColorFrame stack = (GradientColorStack)this.Element; CGColor startColor = stack.StartColor.ToCGColor(); CGColor endColor = stack.EndColor.ToCGColor(); #region for Vertical Gradient var gradientLayer = new CAGradientLayer(); #endregion gradientLayer.Frame = rect; gradientLayer.Colors = new CGColor[] { startColor, endColor }; NativeView.Layer.InsertSublayer(gradientLayer, 0); } }

Now you can Use in PCL Project Like this

<local:GradientColorStack StartColor="#DF596C" EndColor="#FFB239" > </local:GradientColorStack>

Recommend

  • Visual studio 2019 causing slow debugging and high usage of memory
  • Mysteries of C++ optimization
  • How can I search the content of a pdf file in linux shell script? [closed]
  • Using WCF service via async interface from worker thread, how do I ensure that events are sent from
  • How do I hide and show the contents contents of a modal?
  • Intersection of characters in two strings
  • BREAK ON instruction in MySQL queries?
  • How to enable LDAP extension in XAMPP environment
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • Adding Dynamic Row and Data on Checkbox Click
  • Why am I getting an Argument exception when creating event handler dynamically?
  • How can I do case insensitive string search with Linq and SQL Server?
  • Populating a database table with returned JSON
  • Tkinter tkMessageBox disables Tkinter key bindings
  • NoClassDefFoundError: com.google.gson.GsonBuilder when using google play services with microsoft azu
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • How to make Rss News Reader application in android …? [closed]
  • SQL Server version 612 , 655?
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • SpringBoot: Bypass OncePerRequestFilter filters
  • read part of h5 dataset python
  • Spring Boot fails to start
  • Codeigniniter insert data through models and controller
  • VLOOKUP in IMPORTRANGE
  • Sign a Pdf using custom digital signature in Java
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Write to .csv file with PHP (Commas in Data Error)
  • media foundation H264 decoder not working properly
  • Spring Boot not autowiring @Repository
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}