title: Custom Control Causing Null Reference Exception in Xamarin Forms Framework
body: <p>I have built a custom SVG button so that I can have a Xamarin Forms button with text and an SVG image. I have had it working well and then I migrated my project to .NET Standard 2.0. </p>
  
  <p>Now, when changing the text on the control I get a Null Reference Exception inside the Xamarin Forms Framework's StackLayout's LayoutChildIntoBoundingRegion method on a ListDictionaryInternal.</p>
  
  <p>Here is the control. It is pretty simple.</p>
  
  <pre><code>using System.Windows.Input;
  using FFImageLoading.Svg.Forms;
  using Core.Interfaceses;
  using Xamarin.Forms;
  using Xamarin.Forms.Xaml;
  
  namespace CustomControls
  {
      [XamlCompilation(XamlCompilationOptions.Compile)]
      public class SVGImageButton : ContentView
      {
          #region Member Variables
  
          private readonly Button _button;
          private readonly SvgCachedImage _svgImage;
  
          #endregion
  
          #region Bindible Properties
  
          public static BindableProperty ButtonPressedCommandProperty = BindableProperty.Create(nameof(ButtonPressedCommand), typeof(ICommand), typeof(SVGImageButton));
          public ICommand ButtonPressedCommand
          {
              get
              {
                  return (ICommand)GetValue(ButtonPressedCommandProperty);
              }
              set
              {
                  SetValue(ButtonPressedCommandProperty, value);
              }
          }
  
          public static BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(SVGImageButton));
          public object CommandParameter
          {
              get { return GetValue(CommandParameterProperty); }
              set { SetValue(CommandParameterProperty, value); }
          }
  
          public static BindableProperty ButtonTextProperty = BindableProperty.Create(nameof(ButtonText), typeof(string), typeof(SVGImageButton), string.Empty, BindingMode.TwoWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonText = newValue.ToString();
          });
  
          public string ButtonText
          {
              get
              {
                  return _button.Text;
              }
              set
              {
                  _button.Text = value;
              }
          }
  
          public static BindableProperty ButtonBackgroundColorProperty = BindableProperty.Create(nameof(ButtonBackgroundColor), typeof(Color), typeof(View), Color.Fuchsia, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonBackgroundColor = (Color)newValue;
          });
  
          public Color ButtonBackgroundColor
          {
              get
              {
                  return _button.BackgroundColor;
              }
              set
              {
                  _button.BackgroundColor = value;
              }
          }
  
          public static BindableProperty ButtonTextColorProperty = BindableProperty.Create(nameof(ButtonTextColor), typeof(Color), typeof(View), Color.Fuchsia, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonTextColor = (Color)newValue;
          });
  
          public Color ButtonTextColor
          {
              get
              {
                  return _button.TextColor;
              }
              set
              {
                  _button.TextColor = value;
              }
          }
  
          public static BindableProperty ButtonStyleProperty = BindableProperty.Create(nameof(ButtonBackgroundColor), typeof(Style), typeof(View), null, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonStyle = (Style)newValue;
          });
  
          public Style ButtonStyle
          {
              get
              {
                  return _button.Style;
              }
              set
              {
                  _button.Style = value;
              }
          }
  
          public static BindableProperty ButtonBorderWidthProperty = BindableProperty.Create(nameof(ButtonBorderWidth), typeof(double), typeof(View), 0.0, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonBorderWidth = (double)newValue;
          });
  
          public double ButtonBorderWidth
          {
              get
              {
                  return _button.BorderWidth;
              }
              set
              {
                  _button.BorderWidth = value;
              }
          }
  
          public static BindableProperty ButtonBorderColorProperty = BindableProperty.Create(nameof(ButtonBorderColor), typeof(Color), typeof(View), Color.Fuchsia, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =&gt;
          {
              if (newValue == null) return;
              var control = (SVGImageButton)bindable;
              control.ButtonBorderColor = (Color)newValue;
          });
  
          public Color ButtonBorderColor
          {
              get
              {
                  return _button.BorderColor;
              }
              set
              {
                  _button.BorderColor = value;
              }
          }
  
          #endregion
  
          #region Properties
  
          public string SVGImageName { get; set; }
  
          #endregion
  
          #region Life Cycle
  
          protected override void OnParentSet()
          {
              base.OnParentSet();
  
              _button.Text = ButtonText;
              _svgImage.Source = SvgImageSource.FromResource(SVGImageName);
          }
  
          protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
          {
              _button.HorizontalOptions = (Parent as View).HorizontalOptions;
              _button.VerticalOptions = (Parent as View).VerticalOptions;
  
              if (_button.Text != null)
              {
                  var size = DependencyService.Get&lt;ITextMeter&gt;().MeasureTextSize(_button.Text.TrimEnd(), 200, _button.FontSize);
                  WidthRequest = size.Width + 60 + _button.Height;
              }
  
              return base.OnMeasure(widthConstraint, heightConstraint);
          }
  
          public SVGImageButton()
          {
              var content = new RelativeLayout();
  
              _button = new Button { BackgroundColor = Color.Gray, TextColor = Color.Black };
              _svgImage = new SvgCachedImage { IsEnabled = false };
  
              content.Children.Add(_button,
                                   Constraint.RelativeToParent((parent) =&gt;
                                   {
                                       return parent.X;
                                   }),
                                   Constraint.RelativeToParent((parent) =&gt;
                                   {
                                       return parent.Y;
                                   }),
                                   Constraint.RelativeToParent((parent) =&gt;
                                   {
                                       return parent.Width;
                                   }),
                                   Constraint.RelativeToParent((parent) =&gt;
                                   {
                                       return parent.Height;
                                   }));
  
              content.Children.Add(_svgImage,
              Constraint.RelativeToParent((parent) =&gt;
              {
                  return parent.Width - (parent.Height / 2) - (parent.Height / 4);
              }),
              Constraint.RelativeToParent((parent) =&gt;
              {
                  return parent.Height - (parent.Height / 2) - (parent.Height / 4);
              }),
              Constraint.RelativeToParent((parent) =&gt;
              {
                  return parent.Height / 2;
              }),
              Constraint.RelativeToParent((parent) =&gt;
              {
                  return parent.Height / 2;
              }));
  
              Content = content;
  
              _button.Clicked += (sender, e) =&gt;
              {
                  ButtonPressedCommand?.Execute(CommandParameter);
              };
          }
  
          #endregion
  
      }
  }
  </code></pre>
  
  <p>Here is the control's usage in XAML.</p>
  
  <pre><code>&lt;customcontrols:SVGImageButton
                 x:Name="TypeSelectionButton" 
                 HorizontalOptions="Start" 
                 VerticalOptions="Center"
                 ButtonPressedCommand="{Binding TypeButtonClickedCommand}"
                 SVGImageName="SVGImages.ic_triangle_down.svg"
                 CommandParameter="{x:Reference TypeSelectionButton}"
                 ButtonText="{Binding SearchTypeButtonText}"
                 ButtonBackgroundColor="{Binding ButtonBackgroundColor}"/&gt;
  </code></pre>
  
  <p>Before the migration to .NET Standard 2.0, when the text was changed, the control would resize, now it gets this crash.</p>
  
  <p><a href="https://i.stack.imgur.com/dBUHL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dBUHL.png" alt="enter image description here"></a></p>
  
  <p>Any help I can get understanding what is happening is surly appreciated!</p>