您需要在每个平台上创建自定义渲染器。我不知道你是否可以单独控制半径,但你可以控制拐角是否符合半径。
首先,您需要在共享项目中使用一个自定义类来充当您的自定义控件...
using System;
using Xamarin.Forms;
namespace MyApp.Controls
{
public class CustomFrame : Frame
{
// ---------------------------------------------------------------------------------------------------------------
public static readonly BindableProperty CornerRadiusTopLeftProperty = BindableProperty.Create(
propertyName: "CornerRadiusTopLeft",
returnType: typeof(bool),
declaringType: typeof(CustomFrame),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay
);
public bool CornerRadiusTopLeft
{
get { return (bool)GetValue(CornerRadiusTopLeftProperty); }
set { base.SetValue(CornerRadiusTopLeftProperty, value); }
}
// ---------------------------------------------------------------------------------------------------------------
public static readonly BindableProperty CornerRadiusTopRightProperty = BindableProperty.Create(
propertyName: "CornerRadiusTopRight",
returnType: typeof(bool),
declaringType: typeof(CustomFrame),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay
);
public bool CornerRadiusTopRight
{
get { return (bool)GetValue(CornerRadiusTopRightProperty); }
set { base.SetValue(CornerRadiusTopRightProperty, value); }
}
// ---------------------------------------------------------------------------------------------------------------
public static readonly BindableProperty CornerRadiusBottomLeftProperty = BindableProperty.Create(
propertyName: "CornerRadiusBottomLeft",
returnType: typeof(bool),
declaringType: typeof(CustomFrame),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay
);
public bool CornerRadiusBottomLeft
{
get { return (bool)GetValue(CornerRadiusBottomLeftProperty); }
set { base.SetValue(CornerRadiusBottomLeftProperty, value); }
}
// ---------------------------------------------------------------------------------------------------------------
public static readonly BindableProperty CornerRadiusBottomRightProperty = BindableProperty.Create(
propertyName: "CornerRadiusBottomRight",
returnType: typeof(bool),
declaringType: typeof(CustomFrame),
defaultValue: true,
defaultBindingMode: BindingMode.TwoWay
);
public bool CornerRadiusBottomRight
{
get { return (bool)GetValue(CornerRadiusBottomRightProperty); }
set { base.SetValue(CornerRadiusBottomRightProperty, value); }
}
}
}
然后您需要在每个平台上创建渲染器。我还没有完成 Android 端,但这是你需要的 iOS ...
using System;
using CoreAnimation;
using MyApp.iOS.CustomRenderers;
using Foundation;
using MyApp.Controls;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(CustomFrame), typeof(CustomFrameRenderer))]
namespace MyApp.iOS.CustomRenderers
{
public class CustomFrameRenderer : FrameRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (Element != null)
{
var element = Element as CustomFrame;
int result = 0;
if (element.CornerRadiusTopLeft)
result += (int)CACornerMask.MinXMinYCorner;
if (element.CornerRadiusTopRight)
result += (int)CACornerMask.MaxXMinYCorner;
if (element.CornerRadiusBottomLeft)
result += (int)CACornerMask.MinXMaxYCorner;
if (element.CornerRadiusBottomRight)
result += (int)CACornerMask.MaxXMaxYCorner;
Layer.MaskedCorners = (CACornerMask)result;
};
}
}
}
然后,您可以在 XAML 文件中将其用作自定义控件。
将命名空间添加到您的页面...
xmlns:customControls="clr-namespace:MyApp.Controls"
...然后添加您的自定义框架...
<customControls:CustomFrame BackgroundColor="White" CornerRadius="10" HasShadow="false"
CornerRadiusBottomLeft="false" CornerRadiusBottomRight="false" VerticalOptions="Start" HorizontalOptions="FillAndExpand" Padding="15">
<!-- Add your other elements here -->
</customControls:CustomFrame>
希望对你有所帮助。祝 Android 渲染器好运,我相信这并不难,只是还没开始。