1

I am creating custom control in Xamarin with a rectangle in it. I provide a bindable property for the rectangle height in my pcl.

When I pass this height in Android and iOS, the height is different for Android device (Nexus 5 running KitKat) and iOS(iPhone 5 running iOS8.4).

I know that there are concepts like density and screen resolution, DPI and PT, but I don't know what happens when I pass 100 as the height for rectangle in iOS and Android.

Does it considers it as DPI/PT? If not, how can I achieve this?

EDIT:

it is Xamarin Forms code.

Portable project has custom control and a BindableProperty in it with datatype as int named as RectHeight.

Then in customrenderer for Android and iOS, in OnElementChangedProperty method, i write code that assigns height for Rect in Android and CGRect in iOS.

EDIT:

I want to provide the dimension as int in custom control and then put the value in Android and iOS such that the height looks same in both the OSes regardless of screen resolution or density. That's why mentioned DPI and PT

RohitWagh
  • 1,999
  • 3
  • 22
  • 43
  • How did you detect the difference? Because of different display resolutions rectange may seems different. – Ircover Aug 11 '15 at 11:00
  • that what i am saying that different screen resolution may be the reason and if that is the case, i need a universal approach. – RohitWagh Aug 11 '15 at 11:29
  • Is this classic Xamarin or Xamarin Forms? I'm assuming forms. Also when you say the height is different is that the height that is displayed relative to the height of the screen or the actual height as say measured by a ruler? Either way you are looking at some platform specific renderers to achieve this. Do you have code that shows what you have so far? – Kevin Ford Aug 11 '15 at 12:12

1 Answers1

1

Take a look at IDisplay implementations on XLabs. You can get an instance from an IDevice implementation. This will get you the runtime display properties and provides extension methods like getting Forms height/width request values from inches. I wrote them just for this purpose, to make sure I am getting exact dimensions when I need them.

See the Xamarin Forum thread for a sample.

You can either use the source code or install XLabs.Forms package from NuGet. For you purpose you would most likely use the XDpi and YDpi properties to calculate native values. If you don't need any of the other XLabs stuff then I would suggest to copy some of the routines (especially the iOS stuff as Android & WP are much easier to determine at runtime) to your code as extensions maybe.

SKall
  • 5,234
  • 1
  • 16
  • 25