wpf binging 抛出 argumentnullException
当 Visual Studio 中的编辑器(xaml /设计)抛出argumentnullException“值不能为空”时,您如何处理?
我正在使用实体框架从数据库中读取一些值,并将它们存储在可观察的集合中,以便我可以从 xaml 绑定到它们。假设集合中可能没有任何值,因此异常是正确的,这是符合逻辑的,对吗? 即使我在数据库中有值,仍然会引发异常,因为编辑器(在设计模式下查看时)没有运行应用程序来获取值,这仍然是正常的。
当我运行该应用程序时,一切都工作正常,但异常非常烦人。
我还收到一个错误,在从数据库读取时使用 try catch,但仅在编辑器(xaml)中使用,而不是在运行应用程序时使用,该错误表示“指定的命名连接在配置中找不到,不适合与EntityClient 提供程序,或者无效。堆栈跟踪使我进入实体数据模型本身。我使用 mysql 作为后端。 每次我在 xaml 中输入某些内容(字符)时,都会抛出此带有错误的消息框!
我的单元和集成测试都工作正常。
顺便说一句,这是正常的吗?
编辑: 如果我在应用程序资源中添加视图模型的初始化(在 app.xaml.cs 中,但不在 app.xaml 中),它似乎可以工作。当我在 xaml 中编写代码或切换到设计以查看其外观时,我没有看到错误。
How do you handle when the editor (xaml / design) in visual studio throws an argumentnullexception, "value cannot be null" ?
I am reading some values from the database using entity framework and storing them in an observable collection so I can bind to them from xaml. It is logic to suppose that there might not be any values in the collection so the exception is correct, right?
Even if I have values in the database, the exception is still thrown because the editor (when viewing in design mode) is not running the application to get the values, which is still normal.
When I run the application, everything works fine but the exception is very annoying.
I also get an error, using a try catch when reading from the database, but only in editor (xaml) not when running the application, that says "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. The stacktrace gets me into the entity data model itself. I am using mysql as a backend.
This messagebox with the error is thrown everytime I type something (a character) in xaml !
My unit and integration tests all work fine.
Btw is this normal ?
EDIT:
If I add the initialization of the viewmodel in the app resources (in app.xaml.cs but not in the app.xaml) it seems to work. I don't see the errors when I write the code in xaml or when I switch to design to see how it looks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为设计模式设计一些示例数据,以便您可以看到设计器并在xaml中编写代码,而不会出现任何设计器异常。为此,您可能需要使用 DesignerProperties 类(例如
DesignerProperties.GetIsInDesignMode(this)
等)。我遇到过类似的 XAML 设计师迷恋,并使用这种技术解决了问题。我建议阅读此进一步帮助的指南。You can design some sample data for the design mode so that you can see the designer and write code in xaml without any designer exception. For this, you might need to use DesignerProperties class (e.g
DesignerProperties.GetIsInDesignMode(this)
etc). I have faced similar XAML designer crush and solved using this technique. I would recommend reading this guideline for further assistance.您可以调试设计器:-)
为此,请在模型中(在开始处)创建一个断点,
选择 devenv.exe
现在在“调试”下的项目选项中选择“从外部程序开始”,然后在运行项目时 ,将出现一个新的 Visual Studio 实例。当您在新的 Visual Studio 实例中打开窗口时,应该在 Visual Studio 的第一个实例中点击中断
You can debug the designer :-)
For this make a breakpoint in your model (at the beginning)
now in the project options under "Debug" chooose "start with external program" ans select the devenv.exe
now when you run your project, a new visual studio instance will appear. When you open your window in the new visual studio instance, the break should be hit in the first instance of visual studio