升级到带有泛型类型参数的 WPF 4 时,来自 InitializeComponent 的 NullReferenceException
我刚刚开始将所有客户端项目升级到 .NET 4,并且我们正在使用 WPF。我的所有视图都继承自扩展 UserControl 的基类,它是一个仅包含代码的基类,具有单个泛型类型参数:BaseView。在升级到 .NET 4 之前,我只是指定了 x:TypeArgument 并且一切正常,但升级后我现在在 InitializeComponent() 调用上收到 NullReferenceException。 xaml 文件设置为 Page 和 MsBuild:Compile,我尝试将其设置为 Resource,因为我在某处阅读,但这甚至没有构建。如果我创建一个继承自该文件的代码文件(例如 AddressView : BaseView)并使用它,它就可以正常工作。有没有办法继续在我的 XAML 文件中指定类型参数?
更新:添加更深的异常/堆栈跟踪: 你调用的对象是空的。
堆栈跟踪:
位于 System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader、XamlObjectWriter xamlWriter、布尔 onlyLoadOneNode、布尔skipJournaledProperties、布尔 shouldPassLineNumberInfo、IXamlLineInfo xamlLineInfo、IXamlLineInfoConsumer xamlLineInfoConsumer、XamlContextStack`1 堆栈、IStyleConnector styleConnector) 在System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔skipJournaledProperties,Object rootObject,XamlObjectWriterSettings设置,Uri baseUri)
更新2:也许你是对的,我刚刚升级了一个单独的项目,它没有收到该错误在同一台机器上,继承自同一个基类。这非常奇怪,将开始从存在问题的视图中删除项目以尝试找到问题。
I just began upgrading all of our client projects to .NET 4, and we're using WPF. All of my views inherit from a base class that extend UserControl, it is a code only base class with a single generic type argument: BaseView. Prior to upgrading to .NET 4, I simply specified the x:TypeArgument and everything worked fine, but after upgrading I now get a NullReferenceException on the InitializeComponent() call. The xaml file is set to Page and MsBuild:Compile, I tried setting it to Resource as I read somewhere but that doesn't even build. If I create a code file that inherits from that, say AddressView : BaseView, and use that, it works just fine. Is there a way to continue to specify the type arguments in my XAML file instead?
UPDATE: Adding deeper exception/stack trace:
Object reference not set to an instance of an object.
Stack Trace:
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)
UPDATE 2: Maybe you're right, I just upgraded a separate project and it's not getting that error on the same machine, inheriting from the same base class. That is super odd, will start removing items from the view with the issue to try to find the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中似乎抛出了错误 - 您可以通过在调试器中设置更高级别的异常处理来找到此错误。
在 Visual Studio 中,按 Ctrl-Alt-E 打开“异常”窗口,然后确保在两列中都选中“公共语言运行时异常”,然后运行代码。
执行将在发生错误的代码行处中断,从那里修复应该很容易(如果没有,请发布代码和异常文本)。
It looks like an error is being thrown in your code - you can find this by setting a higher level of exception handling in the debugger.
In Visual Studio, key Ctrl-Alt-E to bring up the Exceptions window, then ensure that Common Language Runtime Exceptions is checked in both columns, then run your code.
Execution will break at the line of code where the error is occurring, it should be easy enough to fix from there (if not, post the code and the Exception text).
它是泛型类型参数 - 它曾经在 3.5 中工作。来自http://msdn.microsoft.com/en-us/library/ms750476。 .aspx:
It's the generic type argument - it used to work in 3.5. From http://msdn.microsoft.com/en-us/library/ms750476.aspx: