C# WPF 抛出异常,解析包含 Winforms 用户控件的 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227581/
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
WPF throwing an exception parsing XAML that includes a Winforms User Control
提问by Brian Stewart
I have a WPF app that makes use of a Winforms User Control that I have created using C++/CLI. When my app goes to parse the XAML for my main window, it throws an exception. The information appears to be somewhat abbreviated, but it says:
我有一个 WPF 应用程序,它使用我使用 C++/CLI 创建的 Winforms 用户控件。当我的应用程序为我的主窗口解析 XAML 时,它会引发异常。该信息似乎有些缩写,但它说:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: is not a valid Win32 application. (Exception from HRESULT: 0x800700C1) Error in markup file 'OsgViewer;component/osgviewerwin.xaml' Line 1 Position 9.
I commented out my Winforms control in the XAML and everything loads fine. I figured maybe the constructor for my control is doing something bad, so I put a breakpoint in it, but the breakpoint does not appear to be enabled when I start to run the app, and is never hit, which I understand to mean the DLL containing that line is not loaded. Which would most likely cause an exception to be thrown when an object of a type in the DLL is instantiated - the body of the object's constructor couldn't be found.
我在 XAML 中注释掉了我的 Winforms 控件,并且一切正常。我想也许我的控件的构造函数做错了什么,所以我在里面放了一个断点,但是当我开始运行应用程序时断点似乎没有启用,并且永远不会被命中,我理解这意味着 DLL不加载包含该行的内容。当 DLL 中的某个类型的对象被实例化时,这很可能会导致抛出异常 - 找不到对象构造函数的主体。
I have done this successfully on a different project in the past, so I pulled in a different WinForms User Control from that app, and instantiated it in the XAML, and that all works fine.
我过去曾在不同的项目上成功完成此操作,因此我从该应用程序中提取了不同的 WinForms 用户控件,并在 XAML 中对其进行了实例化,一切正常。
So it's something in this DLL. I have a reference to the DLL in my WPF C# app, and when I load the DLL in Object Browser all the required classes and namespaces show up fine. The app compiles fine, the problem just shows up when parsing the XAML. Anybody seen something like this? Any ideas as to what could be causing this? Ideas for debugging it? Thanks!
所以它是这个 DLL 中的东西。我在 WPF C# 应用程序中有一个对 DLL 的引用,当我在对象浏览器中加载 DLL 时,所有必需的类和命名空间都显示正常。该应用程序编译良好,问题只是在解析 XAML 时出现。有人见过这样的吗?关于可能导致这种情况的任何想法?调试它的想法?谢谢!
<Window x:Class="OsgViewer.OsgViewerWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:int="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:myns="clr-namespace:MyGlobalNS.MyNS;assembly=MyAssembly"
...
<int:WindowsFormsHost x:Name="m_Host">
<myns:CMyClass x:Name="m_MyClass" />
</int:WindowsFormsHost>
...
</window>
采纳答案by EFrank
I have experienced problems like that (but not with the exact same error message). It seems as if WPF cannot instantiate your Winforms User Control.
我遇到过这样的问题(但不是完全相同的错误消息)。似乎 WPF 无法实例化您的 Winforms 用户控件。
The challenge is to find out why. Here are my suggestions that you could try:
挑战在于找出原因。以下是我的建议,您可以尝试:
- Check if you have enabled unmanaged debugging (in Project Properties -> Debug)
- Find out if there are any dependencies your C++/CLI DLL where the Winforms control is implemented and if those dependencies cannot be resolved.
In order to find out dependencies on native DLLs, you should use the tool Dependency Walker (depends.exe). .NET Reflector will only examine managed dependencies. - Comment out code of your Winforms User Control step by step and try again.
- Use Gflags.exe to turn on Loader Snaps(cf. Debugging LoadLibrary Failures)
- 检查您是否启用了非托管调试(在项目属性 -> 调试中)
- 找出实现 Winforms 控件的 C++/CLI DLL 是否存在任何依赖项,以及这些依赖项是否无法解析。
为了找出对本机 DLL 的依赖关系,您应该使用Dependency Walker (depends.exe)工具。.NET Reflector 将仅检查托管依赖项。 - 一步一步注释掉 Winforms 用户控件的代码,然后重试。
- 使用 Gflags.exe 打开加载程序快照(参见Debugging LoadLibrary Failures)
回答by EFrank
I also had this problem and all I had to do was go into the project properties>Security and click the This is a full trust application. I ran my project again and it worked!
我也遇到了这个问题,我所要做的就是进入项目属性>安全性,然后单击这是一个完全信任的应用程序。我再次运行我的项目,它奏效了!
回答by EFrank
Are you sure that you have the dll either in the system32 folder or in the same folder with the exe. I got the exactly same error message when running a WPF project built with CLI dll while the dll was located in the different folder.
您确定您的 dll 位于 system32 文件夹中还是与 exe 位于同一文件夹中。运行使用 CLI dll 构建的 WPF 项目时,我收到了完全相同的错误消息,而 dll 位于不同的文件夹中。
mike
麦克风
回答by Nathan Monteleone
I've seen this problem when trying to use boost::threads. To support thread-local storage, boost::threads makes some Win32 API call that is incompatible with CLI applications. The problem gets triggered if you try to #include something from threads in CLI code.
我在尝试使用 boost::threads 时遇到过这个问题。为了支持线程本地存储,boost::threads 进行了一些与 CLI 应用程序不兼容的 Win32 API 调用。如果您尝试从 CLI 代码中的线程 #include 某些内容,则会触发该问题。
Solution is to either avoid using boost::threads entirely or restrict its use to .cpp files in native code.
解决方案是完全避免使用 boost::threads 或将其使用限制在本机代码中的 .cpp 文件。
回答by Mats
I had simular symptoms and my problem was that the C# project was set to use Any CPU while the C++ project was set to use x86. To set both to use x86 solved the problem
我有类似的症状,我的问题是 C# 项目设置为使用 Any CPU,而 C++ 项目设置为使用 x86。将两者都设置为使用 x86 解决了问题
回答by a52
I also had this execption message, but my solutions was changing the order of XAML elments. I was using a XmlDataProvider and displaying the content in a listbox. I just put the XmlDataProvider before the ListBox.
我也有这个 execption 消息,但我的解决方案正在改变 XAML 元素的顺序。我正在使用 XmlDataProvider 并在列表框中显示内容。我只是将 XmlDataProvider 放在 ListBox 之前。