当我的页面继承自PhoneApplicationPage时,WP7 Silverlight工具包日期选择器抛出空引用异常

时间:2011-08-22 19:46:40

标签: silverlight windows-phone-7 silverlight-toolkit nullreferenceexception

当我使用从PhoneApplicationPage继承的客户页面而不是直接使用它时,datepicker会抛出空引用异常...

我的自定义课程:

public class CustomPhoneApplicationPage : PhoneApplicationPage
{
  public CustomPhoneApplicationPage()
  {
      Loaded += CustomPhoneApplicationPageLoaded;
      IsAnimFromDisabled = false;
      IsAnimToDisabled = false;
      this.Tap += CustomPhoneApplicationPageTap;
      InitTransition();
  }
  private void InitTransition()
  {
    RenderTransform = new CompositeTransform();
    _comeInStoryboard = Application.Current.Resources["FadeIn"] as Storyboard;
    _comeOutStoryboard = Application.Current.Resources["FadeOut"] as Storyboard;
    Debug.Assert(_comeInStoryboard != null, "_comeInStoryboard != null");
    Debug.Assert(_comeOutStoryboard != null, "_comeOutStoryboard != null");
  }
}

在Xaml中使用日期选择器:

<toolkit:DatePicker x:Name="_dateTimePicker" Value="{Binding SnapshotTime, Mode=TwoWay}" />

此处抛出异常:

  

Microsoft.Phone.Controls.Toolkit.dll!Microsoft.Phone.Controls.DataSource.GetPrevious(object relativeTo)+ 0x7 bytes

以下是我如何使用此类作为应用程序中我的页面的基础:

public partial class AddOrEditData : CustomPhoneApplicationPage
{


    public AddOrEditData()
    {
        InitializeComponent();
        Loaded += AddOrEditDataLoaded;

    }
...
}

这是完整的堆栈跟踪:

  

Microsoft.Phone.Controls.Toolkit.dll!Microsoft.Phone.Controls.DataSource.GetPrevious(object relativeTo)第58行+ 0x7字节C#       Microsoft.Phone.Controls.Toolkit.dll!Microsoft.Phone.Controls.Primitives.LoopingSelector.Balance()第146行+ 0xd字节C#       Microsoft.Phone.Controls.Toolkit.dll!Microsoft.Phone.Controls.Primitives.LoopingSelector.UpdateData()第552行+ 0xc0字节C#       Microsoft.Phone.Controls.Toolkit.dll!Microsoft.Phone.Controls.Primitives.LoopingSelector.LoopingSelector_SizeChanged(object sender,System.Windows.SizeChangedEventArgs e)Line 365 C#       System.Windows.dll!System.Windows.FrameworkElement.OnSizeChanged(object sender,System.Windows.SizeChangedEventArgs e)+ 0x15 bytes       System.Windows.dll!MS.Internal.JoltHelper.RaiseEvent(System.IntPtr target,uint eventId,System.IntPtr coreEventArgs,uint eventArgsTypeIndex)+ 0xb2 bytes       [原生于管理过渡]

1 个答案:

答案 0 :(得分:1)

确定,

我通过将这行代码添加到我的继承页面来解决它,以便在离开页面时禁用一些自定义动画:

 protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        IsAnimFromDisabled = e.Uri.OriginalString == @"/Microsoft.Phone.Controls.Toolkit;component/DateTimePickers/DatePickerPage.xaml";
        base.OnNavigatingFrom(e);
    }

它有效!

谢谢大家!