C# “对匹配指定绑定约束的类型'TestWPF.MainWindow' 的构造函数的调用引发了异常。”-如何解决这个问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8987091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:27:30  来源:igfitidea点击:

"The invocation of the constructor on type 'TestWPF.MainWindow' that matches the specified binding constraints threw an exception."- how to fix this?

c#wpfexception

提问by Abdul Ahad

I'm working with WPF. When I'm trying to declare SQLiteConnectionin the code, the problem arises-

我正在使用 WPF。当我试图SQLiteConnection在代码中声明时,问题出现了-

The invocation of the constructor on type 'TestWPF.MainWindow' that matches the specified binding constraints threw an exception.

InnerException: Make sure that the file is a valid .NET Framework assembly.

can anyone tell me, how to fix it?

谁能告诉我,如何解决?

采纳答案by Abdul Ahad

I fixed the problem by adding the below content in app.config,

我通过在中添加以下内容解决了这个问题app.config

<configuration> <startup useLegacyV2RuntimeActivationPolicy="true" /> </configuration>

I found this via a community addition by user FCAAbelow the article " Troubleshooting Exceptions: System.IO.FileLoadException"on MSDN.

我通过用户FCAA在 MSDN 上的文章“排除异常:System.IO.FileLoadException”下方的社区添加发现了这一点。

回答by Brett

In my specific case, I was getting this because I had a few of my referencing assemblies mismatched between x64 and x86. Apparently I was binding to something that needed to be loaded by the runtime.

在我的特定情况下,我得到这个是因为我的一些引用程序集在 x64 和 x86 之间不匹配。显然我绑定到需要由运行时加载的东西。

I mention this here as a reminder to check your build configurations if you've looked everywhere else!

我在这里提到这一点是为了提醒您检查您的构建配置,如果您已经查看过其他任何地方!

回答by Amanda

I got the same error and, after wasting about 2 hours with it, found that it is my SQL Server service that's not running. Not sure if this can ever help someone, but it did solve my problem to just start the service.

我遇到了同样的错误,在浪费了大约 2 个小时之后,发现是我的 SQL Server 服务没有运行。不确定这是否可以帮助某人,但它确实解决了我启动服务的问题。

回答by Jonathan Perry

I had this error in another part of code which has to do with my application resources.

我在与我的应用程序资源有关的另一部分代码中遇到了这个错误。

This was fixed after explicitly setting the ResourcePathfolder in my App.configfile

这是ResourcePath在我的App.config文件中明确设置文件夹后修复的

回答by Priyankara

I had the same problem. i could make it work by renaming the name of App1.config to App.config. I tried all other methods but the solution for me was to change the default name (for me it was App1.config) of the config file to App.config. I shared this because someone may get help by this small modification.

我有同样的问题。我可以通过将 App1.config 的名称重命名为 App.config 来使其工作。我尝试了所有其他方法,但我的解决方案是将配置文件的默认名称(对我来说是 App1.config)更改为 App.config。我分享这个是因为有人可能会通过这个小小的修改得到帮助。

回答by Vivek

Try Adding "Integreted Security = True" in Connection String. It worked for me.

尝试在连接字符串中添加“Integreted Security = True”。它对我有用。

回答by Cosmin Serban

The mentioned exeption is quite generic and you can receive it, for instance, when code fails in the constructor. I had a case of an IO exception that showed up with a similar text. Stepping into the code may provide hints to fix this that may not be obvious otherwise.

提到的例外是非常通用的,例如,当构造函数中的代码失败时,您可以收到它。我有一个出现类似文本的 IO 异常情况。进入代码可能会提供解决此问题的提示,否则可能不明显。

回答by on3al

I ran into this issue and it was caused because my startup application was built as any CPU but I was referencing a project that was built as x64. Setting the startup to build x64 resolved the issue.

我遇到了这个问题,这是因为我的启动应用程序是作为任何 CPU 构建的,但我引用了一个构建为 x64 的项目。将启动设置为构建 x64 解决了该问题。

回答by Alex

My problem was about the interface. I fixed it by deleting the Betternet folder that is located at C:\ProgramData.

我的问题是关于界面。我通过删除位于C:\ProgramData.

Hidden Items/Folders must be shown in order to be able to view the folder.

必须显示隐藏的项目/文件夹才能查看文件夹。

回答by dunwan

In my case it happened in a code-first WPF project. The cause was model changes after restoring a backup, and the error was not being handled appropriately. "The model backing the 'MyDataContext' context has changed since the database was created." Update-Database sorted it out.

就我而言,它发生在代码优先的 WPF 项目中。原因是恢复备份后模型更改,错误没有得到适当处理。“自数据库创建以来,支持‘MyDataContext’上下文的模型已更改。” Update-Database 整理了一下。