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!我不能成爲唯一需要這個的人! :)你的例子像一個魅力工作!我仍然需要找出爲什麼我的項目不能在導航頁上呈現.. –