0
我正在使用MasterDetailPage,其中Menu和Content页面都有白色背景。所以我需要在菜单显示时向内容页面添加阴影分隔符。就像这样:Xamarin Forms在MasterDetailPage内容页面上添加阴影
我能找到的唯一的例子是这样的:https://gist.github.com/SeeD-Seifer/120971b4dda7a785a7f4bda928c9dc2b
我已经实现了代码和阴影效果适用于标签,图像和其他元素。但我无法让它在NavigationPage上工作。
我的代码:
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("MyCompany.PanelShadowEffect")
{
}
}
ShadowNavigationPage.cs
public ShadowNavigationPage(Page root) : base(root)
{
}
protected override void OnAppearing()
{
base.OnAppearing();
Effects.Add(new ShadowEffect()
{
Radius = 0,
DistanceX = -20,
DistanceY = 0,
Color = Color.Black
});
}
PanelShadowEffect
[assembly: ResolutionGroupName("MyCompany")]
[assembly: ExportEffect(typeof(PanelShadowEffect), "PanelShadowEffect")]
namespace MyApp.iOS.Renderer
{
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 = .5f;
control.Layer.MasksToBounds = false;
// This doesn't work either
//Container.Layer.CornerRadius = effect.Radius;
//Container.Layer.ShadowColor = effect.Color.ToCGColor();
//Container.Layer.ShadowOffset = new CGSize(effect.DistanceX, effect.DistanceY);
//Container.Layer.ShadowOpacity = .5f;
//Container.Layer.MasksToBounds = false;
}
catch (Exception ex)
{
Console.WriteLine("Cannot set property on attached control. Error: {0}", ex.Message);
}
}
protected override void OnDetached()
{
}
}
}
嗯,这真的很奇怪!我只是试过这个,但它仍然没有显示出阴影..我会尝试在今晚的新项目中做一个独立的测试,看看我能否在我目前的应用程序中找到问题。非常感谢你的回答!将在几个小时后回来。 –
@MortenOC我上传了一个最小样本项目到我的GitHub [这里](https://github.com/HankiDesign/NavigationPageShadowiOS)。给它一个旋转! – hankide
谢谢!非常好,你也把它添加到Github!我不能成为唯一需要这个的人! :)你的例子像一个魅力工作!我仍然需要找出为什么我的项目不能在导航页上呈现.. –