Reference to Resources property causes an Exception

I have a Xamarin Forms app which is applying 3D rotation to a view. On some devices the default perspective looks really bad so I had to create a custom renderer so that I could specify the camera distance for the view. All of the examples I have seen suggest setting the distance based on the "Resources.DisplayMetrics.Density" value. I got this to work, but for some reason it seems I have to set it repeatedly. Sometimes in the course of grabbing this value I was getting an Exception - usually after backgrounding and foregrounding the app. This is the error:

System.ArgumentException has been thrown
'jobject' must not be IntPtr.Zero.

In the code below the Exception is called on the line where I check whether or not Resources is null. Is there a workaround for this? How can I verify that Resources is "safe" to touch?

`void OnUpdateCamera ()
if (Resources == null)

int distance = 2500;
float scale = Resources.DisplayMetrics.Density;
SetCameraDistance (distance * scale);



  • VitorLeitaoVitorLeitao USMember

    You can check id touching or accessing properties of native elements is safe by checking .Handle == IntPtr.Zero

    If it is true, that means you are trying to access an already disposed element. which means you have a leaky abstraction somewhere.

    But if its enough to know if its safe, checking .Handle works.

Sign In or Register to comment.