如何检查值类型的值是否为IntPtr.Zero

时间:2015-10-21 11:41:36

标签: c# android xamarin mono xamarin.forms

所以我正在编写一个Xamarin应用程序,我已经构建了一个CustomRenderer

现在我的方法:

horizontalScrollView.ScrollTo(this.Width, 0);

不断抛出异常:

  

System.ArgumentException:'jobject'不能是IntPtr.Zero。   10-21 12:10:41.898 E / AndroidRuntime(15053):参数名称:jobject   10-21 12:10:41.898 E / AndroidRuntime(15053):位于/ Users / builder / data / lanes / 2185 / 53fce373 / source / monodroid / src中的Android.Runtime.JNIEnv.CallIntMethod(intptr,intptr)[0x00010] /Mono.Android/src/Runtime/JNIEnv.g.cs:378   10-21 12:10:41.898 E / AndroidRuntime(15053):在Android.Views.View.get_Width()

所以看看异常,从我的断点开始,我发现问题是,当我将鼠标悬停在this.Width上时,我得'jobject' must not be IntPtr.Zero而不是integer,所以我尝试防御性地编码这个和做:

if (this.Width != IntPtr.Zero)
{
   horizontalScrollView.ScrollTo(this.Width, 0);
}

但这不会编译并给我错误:

  

运算符'!='不能应用于'int'类型的操作数   '的IntPtr'

所以我的问题是。有没有办法检查value type是否等于IntPtr.Zero

2 个答案:

答案 0 :(得分:2)

由于您正在使用自定义渲染器,我假设您使用的是Xamarin.Forms。 您无法在此处查看WidthWrite a question here Write another question here Write the third question here 由Xamarin.Forms内部设置。您通常无法访问错误的指针。因此,您无法在那里看到它,但是当您访问A属性时收到错误,因为它在内部使用该指针。

导致此错误的原因有两种:

  1. 您没有正确初始化控件。所以它包含空指针。
  2. Xamarin.Forms中存在一个错误。针对其他版本的Xamarin.Forms测试您的代码。如果错误没有在其他版本中发生,或者您认为它是个错误,请将其归档here

答案 1 :(得分:0)

也许您必须检查作业而不是反对属性值' width'。