Last active
March 11, 2019 17:48
-
-
Save alexshikov/120971b4dda7a785a7f4bda928c9dc2b to your computer and use it in GitHub Desktop.
Shadow for side menu using Xamarin.Forms MasterDetailPage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainMasterDetailPage : MasterDetailPage | |
{ | |
public MainMasterDetailPage () | |
{ | |
Detail = new ShadowNavigationPage(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credits goes to https://github.com/xamarin/xamarin-forms-samples/blob/master/Effects/ShadowEffect/iOS/LabelShadowEffect.cs | |
[assembly: ResolutionGroupName("MyProject")] | |
[assembly: ExportEffect(typeof(PanelShadowEffect), "PanelShadowEffect")] | |
namespace MyProject.iOS | |
{ | |
public class PanelShadowEffect : PlatformEffect | |
{ | |
protected override void OnAttached() | |
{ | |
try | |
{ | |
var effect = (ShadowEffect)Element.Effects.FirstOrDefault(e => e is ShadowEffect); | |
if (effect == null) { | |
return; | |
} | |
var control = Control; | |
if (control == null) | |
{ | |
var renderer = Platform.GetRenderer((VisualElement)Element); | |
control = renderer.ViewController.View; | |
} | |
control.Layer.CornerRadius = effect.Radius; | |
control.Layer.ShadowColor = effect.Color.ToCGColor(); | |
control.Layer.ShadowOffset = new CGSize(effect.DistanceX, effect.DistanceY); | |
control.Layer.ShadowOpacity = 1.0f; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Cannot set property on attached control. Error: {0}", ex.Message); | |
} | |
} | |
protected override void OnDetached() | |
{ | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credits goes to https://github.com/xamarin/xamarin-forms-samples/blob/master/Effects/ShadowEffect/EffectsDemo/ShadowEffect.cs | |
public class ShadowEffect : RoutingEffect | |
{ | |
public float Radius { get; set; } | |
public Color Color { get; set; } | |
public float DistanceX { get; set; } | |
public float DistanceY { get; set; } | |
public ShadowEffect() : base("MyProject.PanelShadowEffect") | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems that in newest versions of forms for some reason Control property of PlatformEffect class is returning a null value I make it works in the next way by using Container property instead