Hi All,
I'm trying to create a gradient view in XF using custom renders.
Here is the code for Gradient view in shared project.
`using Xamarin.Forms;
namespace GradientViewDemo
{
public enum GradientOrientation
{
Vertical = 0,
Horizontal = 1
}
public class GradientView : ContentView
{
public static readonly BindableProperty StartColorProperty = BindableProperty.Create(
propertyName: nameof(StartColor),
returnType: typeof(Color),
declaringType: typeof(GradientView),
defaultValue: Color.Default);
public static readonly BindableProperty EndColorProperty = BindableProperty.Create(
propertyName: nameof(EndColor),
returnType: typeof(Color),
declaringType: typeof(GradientView),
defaultValue: Color.Default);
public static readonly BindableProperty OrientationProperty = BindableProperty.Create(
propertyName: nameof(Orientation),
returnType: typeof(GradientOrientation),
declaringType: typeof(GradientView),
defaultValue: GradientOrientation.Vertical);
public Color StartColor
{
get { return (Color)GetValue(StartColorProperty); }
set { SetValue(StartColorProperty, value); }
}
public Color EndColor
{
get { return (Color)GetValue(EndColorProperty); }
set { SetValue(EndColorProperty, value); }
}
public GradientOrientation Orientation
{
get { return (GradientOrientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
}
}
`
Android renderer:
`[assembly: ExportRenderer(typeof(GradientView), typeof(GradientViewRenderer))]
namespace GradientViewDemo.Droid
{
public class GradientViewRenderer : ViewRenderer
{
private Xamarin.Forms.Color OldStartColor { get; set; }
private Xamarin.Forms.Color OldEndColor { get; set; }
private Xamarin.Forms.Color StartColor { get; set; }
private Xamarin.Forms.Color EndColor { get; set; }
private GradientOrientation Orientation { get; set; }
public GradientViewRenderer(Context context) : base(context) { }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == GradientView.StartColorProperty.PropertyName || e.PropertyName == GradientView.EndColorProperty.PropertyName)
{
UpdateColors();
}
if (e.PropertyName == GradientView.OrientationProperty.PropertyName)
{
UpdateOrientation();
}
}
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
UpdateColors();
UpdateOrientation();
}
private void UpdateColors()
{
if (Element is GradientView element)
{
OldStartColor = StartColor;
OldEndColor = EndColor;
StartColor = element.StartColor;
EndColor = element.EndColor;
}
}
private void UpdateOrientation()
{
if (Element is GradientView element)
{
Orientation = element.Orientation;
}
}
protected override void DispatchDraw(Canvas canvas)
{
var gradient = new LinearGradient(
x0: 0,
y0: 0,
x1: Orientation == GradientOrientation.Horizontal ? Width : 0,
y1: Orientation == GradientOrientation.Vertical ? Height : 0,
color0: StartColor.ToAndroid(),
color1: EndColor.ToAndroid(),
tile: Shader.TileMode.Mirror);
var paint = new Paint
{
Dither = true
};
paint.SetShader(gradient);
canvas.DrawPaint(paint);
base.DispatchDraw(canvas);
}
}
}`
XAML page:
`<?xml version="1.0" encoding="utf-8" ?>
<StackLayout>
<!-- Place new controls here -->
<StackLayout Spacing="40" Margin="20">
<Label Text="test.."/>
<Label Text="test.."/>
<Button Text="Button 1"
Clicked="Handle_Clicked1"/>
<local:GradientView StartColor = "Red"
EndColor = "Yellow"
VerticalOptions="FillAndExpand">
<StackLayout Spacing="40">
<Label Text="test.."/>
<BoxView BackgroundColor="Blue" WidthRequest="200" HeightRequest="100"/>
<Label Text="test.."/>
<Button Text="Button 2"
Clicked="Handle_Clicked2"/>
</StackLayout>
</local:GradientView>
</StackLayout>
</StackLayout>
`
This code works fine in Android 9 emulator on Windows.
But when we run same code on VS Mac page doesn't rendered as expected. Gradient view spreads all over the view. Acts as a input transparent view where we can click hidden elements(Button 1).
This work on older Mac emulators, issue is only with Android 9.0.
Any ideas?
Thanks
Kaushalya