The Frame class in Xamarin Forms is quite limited, and can't allow me to get a shadow behind the Frame. I've made a custom renderer for iOS using this code:
public class RatingInfoFrameRenderer : FrameRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
Layer.BorderColor = UIColor.White.CGColor;
Layer.CornerRadius = 10;
Layer.MasksToBounds = false;
Layer.ShadowOffset = new CGSize(-2, 2);
Layer.ShadowRadius = 5;
Layer.ShadowOpacity = 0.4f;
}
}
Making a similar one on Android is causing me problems, since my knowledge on Android native is kind of limited. Could anyone tell me what to look at, perhaps some good code example? I haven't found anything that looks similar to this.
I was able to get a shadow effect in Xamarin Forms for a box view, I'm pretty sure it can used similarly for a Frame. I got the clue from Android Documentation
I added a new property called HasShadow
Here's the code for the Renderer in Android
And this is how it looks
UPDATE
I found out that this approach causes app crash for older versions of Android. Although I haven't found a way to display Shadows in Android versions prior to Lollipop. This will prevent any app crashes
It can be very easy in Android platform, but first of all, you need to create your shadow under
Drawable
folder of Android resources. For example:Name this file as "shadow.xml" and place it under the
Drawable
folder of Android project, then in yourRatingInfoFrameRenderer
:To change the style of shadow, you can modify the shadow.xml file, for more information about this, you may refer to google's official document: LayerList.
I know this question is old, but there is an updated way of getting a better shadow effect then the accepted answer. inherit from
Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer
and then useSetOutlineSpotShadowColor(Color color)
to set the shadow color. You can useCardElevation
to determine the strength and spread of the shadow as well.Hope this helps someone who stumbles in here like I did.