开发者

System.NullReferenceException when constructing object in .NET 4.0

开发者 https://www.devze.com 2023-02-21 10:22 出处:网络
I just completed the task of migrating a large solution from .NET 3.5 to 4.0.Pretty straight forward and application runs without incident.However, when I try to open one of the child windows, I get t

I just completed the task of migrating a large solution from .NET 3.5 to 4.0. Pretty straight forward and application runs without incident. However, when I try to open one of the child windows, I get the following exception:

Type : System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Message : Object reference not set to an instance of an object. Source : WindowsBase Help link : Data : System.Collections.ListDictionaryInternal TargetSite : System.String AntiFormat(System.String) Stack Trace : at MS.Internal.AvTrace.AntiFormat(String s) at MS.Internal.AvTraceBuilder.AppendFormat(String message, String arg1, String arg2)

at MS.Internal.TraceData.DescribeSourceObject(AvTraceBuilder traceBuilder, Object o) at MS.Internal.TraceData.DescribeTarget(AvTraceBuilder traceBuilder, DependencyObject targetElement, DependencyProperty targetProperty) at MS.Internal.TraceData.Describe(AvTraceBuilder traceBuilder, Object o) at MS.Internal.TraceData.OnTrace(AvTraceBuilder traceBuilder, Object[] parameters, Int32 start) at MS.Internal.AvTrace.Trace(TraceEventType type, Int32 eventId, String message, String[] labels, Object[] parameters) at MS.Internal.TraceData.Trace(TraceEventType type, AvTraceDetails traceDetails, Object p1) at MS.Internal.Data.PropertyPathWorker.ReportNoInfoError(Int32 k, Object parent) at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent) at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange) at MS.Internal.Data.ClrBindingWorker.AttachDataItem() at System.W开发者_开发百科indows.Data.BindingExpression.Activate(Object item) at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt) at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp) at System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp) at System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)

at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Baml2006.WpfMemberInvoker.SetValue(Object instance, Object value) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)

at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value) at System.Xaml.XamlObjectWriter.SetValue(Object inst, XamlMember property, Object value) at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent) at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx) at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx) at System.Xaml.XamlObjectWriter.WriteEndObject() at System.Xaml.XamlWriter.WriteNode(XamlReader reader) at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)

at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at OCC600.LogManager.LogViewer.InitializeComponent() in c:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Views\LogViewer.xaml:line 1 at OCC600.LogManager.LogViewer..ctor(LogsViewModel model, LogMessage log) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Views\LogViewer.xaml.cs:line 31 at OCC600.LogManager.Models.LogsViewModel.CreateDataItemDetailsWindow(LogMessage dataItem, Int64 key) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Models\LogsViewModel.cs:line 163 at ControlsLibrary.wpf.GenericViewModel`2.OpenDataItemDetailsWindow(T dataItem, Int64 windowKey, Boolean trackWindow) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\Libraries\ControlsLibrary\Utility\GenericViewModel.cs:line 163 at OCC600.LogManager.Models.LogsViewModel.OpenLogDetailsWindow(LogMessage log) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Models\LogsViewModel.cs:line 190 at OCC600.LogManager.Models.LogsViewModel.DisplayDetails(IEnumerable data, Boolean trackWindows) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Models\LogsViewModel.cs:line 176 at OCC600.LogManager.LogsView.logDataGrid_PreviewMouseDoubleClick(Object sender, MouseButtonEventArgs e) in C:\dev\LUT600 2.1.1\OCC600\DotNet4\Source\LogManager\LogManager\Views\LogsView.xaml.cs:line 201 at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.Controls.Control.OnMouseDoubleClick(MouseButtonEventArgs e) at System.Windows.Controls.Control.HandleDoubleClick(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

The offending line is:

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

defined in the following context:

<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>    

This exception is thrown during the call to InitializeComponent() defined in the following constructor:

 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;
        } 

I do not have this problem in .NET 3.5. Any ideas on how I go about fixing this?


The line that is throwing the error looks like it comes from a custom control, it could be that the TextViewer control does not like .net 4. If you have access to the source code then I would try to compile/test that control seperately from the rest of your project to isolate the issue.


If I use add an x:Name attribute to my custom control in the XAML, problem goes away.

So,

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

does not throw an exception while:

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

does.

0

精彩评论

暂无评论...
验证码 换一张
取 消