Форсировать перерисовку Xamarin.Forms View с помощью настраиваемого средства визуализации
У меня есть визуальный элемент 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);
}
}
}
}
При нажатии кнопки красная граница должна стать синей. По-видимому, рендер не замечает измененное свойство. Как я могу вызвать перерисовку?
![GsLd6.png]()
(Этот пример для iOS. Но мой вопрос относится и к Android.)
Ответы
Ответ 1
Требуются две модификации:
-
Вызвать OnPropertyChanged
в настройщике свойства BoundaryColor
:
public class MyButton: Button
{
Color boundaryColor = Color.Red;
public Color BoundaryColor {
get {
return boundaryColor;
}
set {
boundaryColor = value;
OnPropertyChanged(); // <-- here
}
}
}
-
Подпишитесь на событие в OnElementChanged
методе MyButtonRenderer
:
public class MyButtonRenderer: ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
Element.PropertyChanged += (s_, e_) => SetNeedsDisplay(); // <-- here
}
public override void Draw(RectangleF rect)
{
// ...
}
}
Примечание:
Кажется, важно подписаться в OnElementChanged
, а не на конструктор. В противном случае создается a System.Reflection.TargetInvocationException
.
Ответ 2
Сначала верните BoundaryColor
в свойство bindable. Чтобы этого не требовалось, достаточно запустить событие INPC
, но тогда вы можете привязать его к нему:
public static readonly BindableProperty BoundaryColorProperty =
BindableProperty.Create ("BoundaryColor", typeof(Color), typeof(MyButton), Color.Default);
public Color BoundaryColor {
get { return (Color)GetValue (BoudaryColorProperty); }
set { SetValue (BoundaryColorProperty, value); }
}
то в вашем рендерере:
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == MyButton.BoundaryColorProperty.PropertyName)
SetNeedsDisplay ();
}