Xamarin Forms native customrenderer
问题描述:
我跟随了James Montemagno的指南,介绍如何为我的Xamarin Forms共享项目中的圆形图像制作自定义渲染器。Xamarin Forms native customrenderer
https://blog.xamarin.com/elegant-circle-images-in-xamarin-forms/
(为导向的真实副本,感觉多余的实际代码本身添加到我的项目,但请评论,如果事实并非如此)
据工作完美无瑕,不过,我需要在应用程序运行时按下按钮来动态更改圆形边框的颜色。
但是由于圆形的颜色是在每个渲染器中本地设置的,我不确定我可以如何从共享代码中更改它。
答
也许这个片段可以帮助:
public class CircleImage : Image
{
public static readonly BindableProperty CurvedBackgroundColorProperty =
BindableProperty.Create(
nameof(CurvedBackgroundColor),
typeof(Color),
typeof(CurvedCornersLabel),
Color.Default);
public Color CurvedBackgroundColor
{
get { return (Color)GetValue(CurvedBackgroundColorProperty); }
set { SetValue(CurvedBackgroundColorProperty, value); }
}
}
//Android/iOS
[assembly: ExportRenderer(typeof(CircleImage), typeof(CircleImageRenderer))]
namespace SchedulingTool.iOS.Renderers
{
public class CircleImageRenderer : ImageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var xfViewReference = (CircleImage)Element;
//Here you can reference xfViewReference.CurvedBackgroundColor to assign what ever is binded.
}
}
}
}
我希望你主要的想法,你可以创建自己的可绑定属性和访问他们在本地渲染。
如果如预期,你可以随时下载的NuGet(已你需要的一切)一切不走:
为什么不公开一个属性来设置颜色? – Jason
优秀的建议,绝对理想。我自己的尝试是徒劳的,因此是个问题。 –
[ImageCirclePlugin](https://github.com/jamesmontemagno/ImageCirclePlugin)已经有一个'BorderColor',您可以在共享代码中进行更新。渲染器将检测到更改并相应更新。 – Ada