Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Frame Shadow Design

I have an Xamarin Content Page with a List. For the ListItems I want something similar to the cardview in Android.

enter image description here

Base on what I found that could be accomplished by a Frame. I have this code:

<ViewCell>
    <StackLayout Padding="8" >
        <controls:CardView HasShadow="True" OutlineColor="LightGray">
            <StackLayout Orientation="Vertical" Padding="5">
                // Some labels and Buttons                
            </StackLayout>
        </Frame>
    </StackLayout>
</ViewCell>

The CardView has the following code:

public class CardView : Frame
{
    public CardView()
    {
        Padding = 0;
        if (Device.RuntimePlatform == Device.iOS)
        {
            HasShadow = false;
            OutlineColor = Color.Transparent;
            BackgroundColor = Color.Transparent;
        }
    }
}

This results in a something like this:

enter image description here

This looks more like a Border than this card levitation effect. The example above actually uses the same cardview control, without any styles (even without the OutlineColor). Do I miss a option I have to configure? Or how could I achieve the same result as in the sample?

Xamarin.Forms Version: 2.5.0.280555

like image 865
NPadrutt Avatar asked Mar 05 '18 12:03

NPadrutt


2 Answers

I have implemented something very similar (also Frames as cards to be displayed in a stack view). Unfortunately I can't share the exact code, for it's not me owning it, but my employer, but I can tell you how to achieve this.

I have added a property ShadowRadius to CardView and created a custom renderer, derived from Xamarin.Forms.Platform.Android.AppCompat.FrameRenderer. In the renderer I am setting the Elevation of the renderer

protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
    /* ... */

    this.Elevation = ((CardView)e.NewElement).ShadowRadius;
}

My cards are showing a nice elevation shadow with Xamarin.Forms 2.5.0.280555.

like image 109
Paul Kertscher Avatar answered Sep 19 '22 16:09

Paul Kertscher


You can try this

<Frame HasShadow="True">
    <controls:CardView  OutlineColor="LightGray">
            <StackLayout Orientation="Vertical" Padding="5">
                // Some labels and Buttons                
            </StackLayout>
    </controls:CardView>
</Frame>
like image 37
Sumit Pathak Avatar answered Sep 23 '22 16:09

Sumit Pathak