【发布时间】:2014-09-27 09:13:36
【问题描述】:
我有一个视觉元素 MyButton,带有为 iOS 实现的自定义渲染器。
共享:
namespace RendererTest
{
public class MyButton: Button
{
public Color BoundaryColor { get; set; }
}
public static class App
{
public static Page GetMainPage()
{
var button = new MyButton { Text = "Click me!", BoundaryColor = Color.Red };
button.Clicked += (sender, e) => (sender as MyButton).BoundaryColor = Color.Blue;
return new ContentPage { Content = button };
}
}
}
iOS:
[assembly:ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace RendererTest.iOS
{
public class MyButtonRenderer: ButtonRenderer
{
public override void Draw(RectangleF rect)
{
using (var context = UIGraphics.GetCurrentContext()) {
context.SetFillColor(Element.BackgroundColor.ToCGColor());
context.SetStrokeColor((Element as MyButton).BoundaryColor.ToCGColor());
context.SetLineWidth(10);
context.AddPath(CGPath.FromRect(Bounds));
context.DrawPath(CGPathDrawingMode.FillStroke);
}
}
}
}
按下按钮时,红色边界应变为蓝色。显然渲染器没有注意到更改的属性。如何触发重绘?
(此示例适用于 iOS。但我的问题也适用于 Android。)
【问题讨论】:
标签: android ios xamarin xamarin.forms custom-renderer