WPF自定义控件XAML null错误
另一个奇怪的 WPF 错误:
我在简单的 XAML 页面中有一个自定义控件。该项目构建并运行完美,没有任何错误。
这是 XAML:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:GraphCanvas x:Name="graphCanvas" Width="700" Height="500" Background="#ddd" />
</StackPanel>
但是,当我尝试在 XAML 设计器窗口中查看 XAML 页面时,我收到消息:
“加载时出现问题 - 文档包含必须修复的错误...”
当我点击“重新加载设计器”链接时,错误列表中会弹出一个错误,其中显示:
“对象引用未设置到对象的实例”
并且以“graph:GraphCanvas...”开头的行带有下划线。
我最初是在我们的开发服务器上进行开发的,在那里,一切都是一样的,除了错误是:
“请求“System.Security.Permissions.SecurityPermission”类型的权限(...等等等等...失败。)”
再次,一切正常,除了我无法在 XAML 设计器窗口中查看页面,这使我无法开发应用程序的其余部分。
我尝试过清理、构建、重建以及这些命令的所有命令组合,但没有成功。
编辑:
请记住,这是我的第一个自定义控件,我可能会做一些非常非常错误的事情。就像我说的,该应用程序编译和运行得非常漂亮,所以我有点困惑。
编辑#2:
我的派生画布不是部分类。根据我对 InitializeComponent
工作原理的理解(InitializeComponent() 的作用是什么?它在 WPF 中如何工作?),没有自动生成的包含链接到我的控件的 InitializeComponent
的分部类。同样,除了破坏 XAML 设计视图之外,控件在没有它的情况下也可以完美运行。
Yet another strange WPF error:
I have a custom control in a simple XAML page. The project builds and runs perfectly, with no errors.
Here's the XAML:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:GraphCanvas x:Name="graphCanvas" Width="700" Height="500" Background="#ddd" />
</StackPanel>
But when I try to view the XAML page in the XAML designer window, I get the message:
"Problem Loading--The document contains errors that must be fixed..."
When I hit the "Reload the designer" link, an error pops up in the Error List which says:
"Object reference not set to an instance of an object"
and the line starting with "graph:GraphCanvas..." is underlined.
I was originally developing on our development server, and there, everything was the same, except the error was:
"Request for the permission of type 'System.Security.Permissions.SecurityPermission (...blah blah... failed.)"
Again, everything works perfectly, except that I can't view the page in the XAML designer window, which is keeping me from developing the rest of the app.
I've tried cleaning, building, rebuilding, and all combinations of orders of these commands, with no success.
EDIT:
Please bear in mind this is my first custom control, and I could be doing something horribly, horribly wrong. Like I said, the app compiles and works beautifully, so I'm kinda mystified.
EDIT #2:
My derived canvas is not a partial class. From my understanding of how InitializeComponent
works (What does InitializeComponent() do, and how does it work in WPF?), there is no auto-generated partial class that contains InitializeComponent
linked to my control. Again, the control works perfectly without it, except for breaking the XAML design view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最近有类似的问题。基本上我的理解是,在设计器中,有些事情发生的顺序与运行时不完全相同,并且您认为永远不会为空的一些事情实际上在设计时为空。
我这样解决了这个问题:在我的控制中注释掉大部分代码,直到 Cider 中没有错误为止,然后取消注释它们,直到我再次遇到错误。然后,当错误源被定位时,我用
Even 包围了有问题的部分,当我觉得不可能为空时。一段时间后我摆脱了这个错误。
不是很“科学”的方法,但它解决了我的问题。 :)
I had a similar issue recently. Basically my understanding is that in designer some things happens in not exactly the same order as during run-time and some things that you would think could never be null actually are null during design-time.
I solved the problem this way: commented large parts of code in my control to the point there was no error in Cider and then uncommented them until I got the error again. Then when source of the error was localized, I surrounded problematic parts with
Even when I felt that there's no way that could be null. And after some time I got rid of the error.
Not very "scientific" approach but it solved my problem. :)
不幸的是,Visual Studio 2008 中的 Cider 设计器存在问题,这种类型的错误非常常见。 VS2010 中的版本有了很大的改进,但是当您在 VS2008 中遇到这个问题时,这并不令人放心。
Unfortunately, there are problems with the Cider designer in Visual Studio 2008 where this type of error is all too common. The version in VS2010 is vastly improved, but that's no comfort when you hit this issue in VS2008.
我不确定“Grapher2”是什么,但您可以检查以确保其构造函数中没有代码在设计器实例化它时无法正确运行。像数据库调用之类的事情会出现问题。
I'm not sure what "Grapher2" is but you might check to make sure that there isn't code in its constructor that can't run correctly when the designer instantiates it. Something like a database call would be problematic.
我见过类似的问题。
这只是部分解决方案,因为它不会在设计模式下在父级中呈现,但它会消除错误。这是迄今为止我能找到的最好的解决方案。
在自定义控件的构造函数中。
希望对您有所帮助,或者也许会帮助您找到更好的解决方案。如果您这样做,请发布它,以便我可以使用它;)
编辑
不使用上面的代码时出现不同的错误:
无法创建类型为“”的实例,
因此可能是与我所看到的不同的问题,但听起来可能相关。
I've seen similar issues.
This is only a partial solution as it won't render in the parent in design mode, but it'll get rid of the error. It's the best solution I've been able to find so far.
In the Constructor of your custom control.
Hope that helps, or maybe it'll help you find a better solution. If you do please post it so I can use it ;)
Edit
I get a different error when not using the code above:
Could not create an instance of type ''
So may be a different issue then what I've seen, but sounds like it might be related.