我试图找出一个问题,该问题仅在 Release模式下发生,并且很可能是由于某些属性的无效混淆引起的。我知道初始化特定控件时会发生这种情况,但是此控件很大。我花了一天时间浏览所有XAML和绑定(bind),但仍然看不到是什么导致了此异常。

是否可以获取更多信息。要知道是什么导致了此异常?

Exception : System.NullReferenceException
Message   : Object reference not set to an instance of an object.
Source    : PresentationFramework
Help      :
Stack     :
   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 MyClass.InitializeComponent()

最佳答案

我不知道获取更详细的异常消息的方法,但是它至少对其他人了解可能的原因很有用。我刚刚将NullReferenceException中的WpfXamlLoader.TransformNodes跟踪到已向DependencyProperty注册的DependencyProperty.Register(string, Type, Type)。改变中

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
        nameof(Foo), typeof(object), typeof(Bar));



public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
        nameof(Foo), typeof(object), typeof(Bar), new FrameworkPropertyMetadata(null));

解决了问题。

09-28 08:02