2011-03-30 49 views
1

我刚刚完成了将大型解决方案从.NET 3.5迁移到4.0的任务。非常直接,应用程序运行时没有发生事故然而,当我尝试打开子窗口之一,我得到以下异常:在.NET 4.0中构造对象时System.NullReferenceException

类型信息:System.NullReferenceException, mscorlib程序,版本= 4.0.0.0, 文化=中立, 公钥= b77a5c561934e089 消息:对象引用未设置为 对象的实例。来源: WindowsBase帮助链接:数据: System.Collections.ListDictionaryInternal TargetSite:System.String AntiFormat(System.String)堆栈跟踪 :在 MS.Internal.AvTrace.AntiFormat在 MS(字符串 S)。 Internal.AvTraceBuilder.AppendFormat(字符串 消息,字符串ARG1,字符串ARG2)
在 MS.Internal.TraceData.DescribeSourceObject(AvTraceBuilder traceBuilder,对象o)在 MS.Internal.TraceData.DescribeTarget(AvTraceBuilder traceBuilder,DependencyObject的 targetElement,DependencyProperty targetProperty)处 MS.Internal.TraceData.OnTrace(AvTraceBuilder traceBuilder MS.Internal.TraceData.Describe(AvTraceBuilder traceBuilder,对象o),对象[]参数, 的Int32开始)在 MS.Internal.AvTrace .Trace(TraceEventType 型,的Int32 EVENTID,字符串信息, 字符串[]标签,对象[]参数) 处 MS MS.Internal.TraceData.Trace(TraceEventType 类型,AvTraceDetails traceDetails, 对象P1)。 Internal.Data.PropertyPathWorker.ReportNoInfoError(Int32 k,Object parent)at MS.Internal.Data .PropertyPathWorker.ReplaceItem在 MS.Internal.Data(的Int32 K,对象newO,父对象)在 MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(的Int32 K,ICollectionView的CollectionView, 对象NEWVALUE,布尔 isASubPropertyChange)。 ClrBindingWorker.AttachDataItem() 处 System.Windows.Data.BindingExpression.AttachOverride System.Windows.Data.BindingExpression.Activate(对象 项目)在 System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt 尝试)(目标,DependencyProperty dp) System.Windows.Data.BindingEx pressionBase.Attach(DependencyObject的 目标,的DependencyProperty DP)在 System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject的 d,的DependencyProperty DP)在 System.Windows.DependencyObject.SetValueCommon(的DependencyProperty DP,对象的值,PropertyMetadata 元数据布尔 coerceWithDeferredReference,布尔 coerceWithCurrentValue,operationType operationType,布尔isInternal)
在 System.Windows.DependencyObject.SetValue在 (的DependencyProperty DP,对象的值)System.Windows.Baml2006.WpfMemberInvoker.SetValue(对象 instance,Object value)at MS.Internal.Xaml.Runtime。ClrObjectRuntime.SetValue(XamlMember 构件,对象OBJ,对象的值)
在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(对象 研究所,XamlMember属性,对象 值) System.Xaml.XamlObjectWriter.SetValue在在 System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext CTX) System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext CTX,XamlMember道具,对象的值,布尔 onParent)(对象 研究所,XamlMember属性,对象 值)在 System.Xaml.XamlObjectWriter.Logic_ AssignProvidedValue(ObjectWriterContext CTX)在 System.Xaml.XamlObjectWriter.WriteEndObject() 在 System.Xaml.XamlWriter.WriteNode(XamlReader 阅读器)在 System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader,XamlObjectWriter 的XamlWriter,布尔onlyLoadOneNode, 布尔skipJournaledProperties, 布尔shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1栈, IStyleConnector styleConnector)在 System.W indows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔 skipJournaledProperties,对象 rootObject,XamlObjectWriterSettings 设置,乌里基本URI)在 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,布尔 skipJournaledProperties在 System.Windows.Markup.XamlReader.LoadBaml(流 流,ParserContext parserContext, 对象的父,布尔closeStream)
对象 rootObject,XamlAccessLevel ACCESSLEVEL,乌里基本URI)在 SY在c:\ dev \ LUT600中的OCC600.LogManager.LogViewer.InitializeComponent() 2.1.1 \ OCC600 \ DotNet4 \ Source \ LogManager \ LogManager \ Views中的stem.Windows.Application.LoadComponent(Object component,Uri resourceLocator) \ LogViewer.xaml: 1在 OCC600.LogManager.LogViewer..ctor(LogsViewModel 模型,的LogMessage日志)中 C线:\ dev的\ LUT600 2.1.1 \ OCC600 \ DotNet4 \源\日志管理\日志管理\观\ LogViewer.xaml.cs:线 OCC600.LogManager.Models.LogsViewModel.CreateDataItemDetailsWindow(的LogMessage 的DataItem,Int64的键)在C:\ dev的\ LUT600 2.1.1 \ OCC600 \ DotNet4 \源\日志管理\日志管理\型号\ LogsViewM odel.cs:线 163在 ControlsLibrary.wpf.GenericViewModel`2.OpenDataItemDetailsWindow(T 的DataItem,Int64类型windowKey,布尔 trackWindow)在C:\ dev的\ LUT600 2.1.1 \ OCC600 \ DotNet4 \源\库\ ControlsLibrary \实用\ GenericViewModel.cs:线 163在 OCC600.LogManager.Models.LogsViewModel.OpenLogDetailsWindow(的LogMessage 日志)在C:\ dev的\ LUT600 2.1.1 \ OCC600 \ DotNet4 \源\日志管理\日志管理\模型\ LogsViewModel.cs:line 190 at OCC600.LogManager.Models.LogsViewModel.DisplayDetails(IEnumerable data,Boolean trackWindows)in C:\ dev \ LUT600 2.1。在 C线 176在 OCC600.LogManager.LogsView.logDataGrid_PreviewMouseDoubleClick(对象 发件人,MouseButtonEventArgs E)::1个\ OCC600 \ DotNet4 \源\日志管理\日志管理\模型\ LogsViewModel.cs \ dev的\ LUT600 2.1.1 \ OCC600 \ DotNet4 \源\日志管理\日志管理\视图\ LogsView.xaml.cs:线 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(代表 genericHandler,对象genericTarget) 在 System.Windows.RoutedEventArgs。 InvokeHandler(代理 handler,Object target)at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs 个routedEventArgs)在 System.Windows.EventRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs指定参数时,布尔 再加注)在 System.Windows.UIElement.RaiseEventImpl(DependencyObject的 发件人,RoutedEventArgs参数)在 System.Windows.UIElement.RaiseEvent (RoutedEventArgs e)上在 System.Windows.Controls.Control.HandleDoubleClick(对象 发件人,MouseButtonEventArgs E) System.Windows.Controls.Control.OnMouseDoubleClick(MouseButtonEventArgs e)上 System.Windows.Input.MouseButtonEventArgs。 InvokeEventHandler(委托 genericHandler,对象genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(代表 处理程序,对象目标)在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标,RoutedEventArgs routedEventArgs)在 System.Windows.EventRoute.InvokeHandlersImpl(对象 源, RoutedEventArgs指定参数时,布尔 再加注)在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject的 发件人,RoutedEventArgs指定参数时, RoutedEvent newEvent)在 System.Windows.UIElement.OnMouseDownThunk(对象 发件人,MouseButtonEventArgs e)上 System.Windows .Input.MouseButtonEventArgs.InvokeEventHandler在 System.Windows处 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标,RoutedEventArgs routedEventArgs) System.Windows.RoutedEventArgs.InvokeHandler(代表 处理程序,对象目标)(代表 genericHandler,对象genericTarget) 。 EventRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs指定参数时,布尔 再加注)在 System.Windows.UIElement.RaiseEventImpl在 System.Windows.UIElement.RaiseTrustedEvent在 系统(RoutedEventArgs 参数)(的DependencyObject 发件人,RoutedEventArgs参数)。 Windows.UIElement .RaiseEvent(RoutedEventArgs 指定参数时,布尔信任)在 System.Windows.Input.InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入)在 System.Windows.Input.InputProviderSite。 ReportInput(inputReport inputReport)在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr的 HWND,INPUTMODE模式,的Int32时间戳, RawMouseActions动作的Int32的x, 的Int32 Y,的Int32轮)在 System.Windows.Interop。 HwndMouseInputProvider。FilterMessage(IntPtr的 HWND,WindowMessage味精,IntPtr的 wParam中,IntPtr的lParam的,布尔& 处理)在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr的 HWND,MSG的Int32,IntPtr的wParam中,IntPtr的 lParam的,布尔&处理)在 MS.Win32.HwndWrapper.WndProc(IntPtr的 HWND,MSG的Int32,IntPtr的wParam中,IntPtr的 lParam的,布尔&处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 O)在 System.Windows.Threading程序.ExceptionWrapper.InternalRealCall(委托 回调,Object args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源,委托方法,对象指定参数时, 的Int32 numArgs,代表catchHandler)

有问题的行是:

<cdic:TextViewer Text="{Binding Message}" Grid.Row="1"/> 

定义在以下上下文中:

<Grid Name="dGrid" > 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Border Margin="0,5,0,5" >     
      </Border> 
      <cdic:TextViewer Text="{Binding Message}" Grid.Row="1"/>    
     </Grid>  

在调用t期间抛出此异常Ø的InitializeComponent()定义在下面的构造:

public LogViewer(LogsViewModel model, LogMessage log) 
     { 
      this.myKey = log.Key; 
      this.DataContext = model;        
      InitializeComponent(); 

      dGrid.DataContextChanged += delegate 
      { 
       if (dGrid.DataContext != null) 
        this.Title = string.Format(LocalizedStrings.LogDetails + ": {0}", 
         (dGrid.DataContext as LogMessage).ToString()); 
      }; 

      dGrid.DataContext = log; 
     } 

我不NET 3.5也有这个问题。关于我如何解决这个问题的任何想法?

回答

2

如果我在XAML中使用添加x:Name属性到我的自定义控件,问题就会消失。

所以,

<cdic:TextViewer x:Name="textViewer" Text="{Binding Message}" Grid.Row="1"/> 

而没有抛出异常:

<cdic:TextViewer Text="{Binding Message}" Grid.Row="1"/>  

一样。

2

引发错误的行看起来像来自自定义控件,可能是TextViewer控件不像.net 4.如果您有权访问源代码,那么我会尝试编译/测试从您的项目的其余部分独立控制来隔离问题。

+0

只要我使用绑定,就会在微软代码中抛出异常。 – 2011-03-30 21:02:50