Force redraw of Xamarin.Forms View with custom renderer

前端 未结 2 685
小鲜肉
小鲜肉 2021-02-13 18:16

I have a visual element MyButton with a custom renderer implemented for iOS.

Shared:

namespace RendererTest
{
    public class MyButton: But         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 18:43

    Two modifications were required:

    1. Call OnPropertyChanged within the setter of the BoundaryColor property:

      public class MyButton: Button
      {
          Color boundaryColor = Color.Red;
      
          public Color BoundaryColor {
              get {
                  return boundaryColor;
              }
              set {
                  boundaryColor = value;
                  OnPropertyChanged();  // <-- here
              }
          }
      }
      
    2. Subscribe to the event within the OnElementChanged method of MyButtonRenderer:

      public class MyButtonRenderer: ButtonRenderer
      {
          protected override void OnElementChanged(ElementChangedEventArgs

    Note: It seems to be important to subscribe within OnElementChanged and not the constructor. Otherwise a System.Reflection.TargetInvocationException is raised.

提交回复
热议问题