15

MyButtoniOS 用にカスタム レンダラーが実装されたビジュアル要素があります。

共有:

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 にも当てはまります。)

4

2 に答える 2

10

まず、あなたBoundaryColorをバインド可能なプロパティに変えます。これは必須ではありません。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 ();
}
于 2014-08-05T07:14:26.790 に答える
9

2 つの変更が必要でした。

  1. プロパティOnPropertyChangedのセッター内で呼び出します。BoundaryColor

    public class MyButton: Button
    {
        Color boundaryColor = Color.Red;
    
        public Color BoundaryColor {
            get {
                return boundaryColor;
            }
            set {
                boundaryColor = value;
                OnPropertyChanged();  // <-- here
            }
        }
    }
    
  2. 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コンストラクターではなく、内部で サブスクライブすることが重要なようです。それ以外の場合は aSystem.Reflection.TargetInvocationExceptionが発生します。

于 2014-08-05T18:29:54.820 に答える