Search code examples
xamarinxamarin.formsuwpheightwidth

How would i pass element's width and height to an effect or a custom renderer in XamarinForms


I'm not able to get the width nor the height of an element in the effects attaching to the element. I tried every single way:

void UpdateSize()
{
      var _1 = ((BoxView)Element).Width; //-1
      var _2 = ((BoxView)Element).WidthRequest; //-1
      var _3 = ((BoxView)Element).Height; //-1
      var _4 = ((BoxView)Element).HeightRequest; //-1
      var _5 = Control.Width; //NaN (UWP). NaN is not a number
      var _6 = Control.ActualWidth; //0
      var _7 = Control.Height; //Nan (UWP). NaN is not a number
       var _8 = Control.ActualHeight; //0
}

I even tried to pass the width and the height as a attached properties to the effect. I need the width and height because i'm creating a drop shadow and I must resize the SpriteVisual to be the same size as the element.

May be the element draws after applying the renderer/effects so we have no size yet. If so, how to work around with this?


Solution

  • I would suggest to wire-up the SizeChanged event of Element when effect is attached and then creating your shadow once it fires. Also, make sure to unsubscribe the event when the effect is detached.