windows phone使用GestureService动态抛出nullreferenceexcpetion

时间:2014-05-29 02:21:59

标签: windows-phone-8 nullreferenceexception toolkit

我使用工具包GestureService动态,下面的代码放在页面构造中是可以的,但是在Grid的控件中,StackPanel会抛出nullreferenceexcpetion。

private void TitlePanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e) {
    Image img = new Image();
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.UriSource = new Uri("/Assets/background.png", UriKind.Relative);
    img.Source = bitmapImage;
    var gesture = GestureService.GetGestureListener(img);
}

异常堆栈跟踪是:

at Microsoft.Phone.Controls.GestureListener.RaiseGestureEvent[T](Func`2 eventGetter, Func`1 argsGetter, Boolean releaseMouseCapture)
at Microsoft.Phone.Controls.GestureListener.OnTap(Object sender, GestureEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.ni.dll

2 个答案:

答案 0 :(得分:0)

将您的代码修改为:

private void TitlePanel_Tap_1(object sender, System.Windows.Input.GestureEventArgs e) {
    Image img = new Image();        
    img.Source = new BitmapImage(new Uri("/Assets/background.png", UriKind.Relative));;
    if(null != img)
    {
          var gesture = GestureService.GetGestureListener(img);
          //Apply your tap events for gesture if you need here
    }
}

答案 1 :(得分:0)

您应该尝试使用this代码。它应该适用于Grid。