7

我有一个 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.

我在 XAML 中注释掉了我的 Winforms 控件,一切正常。我想也许我的控件的构造函数做错了,所以我在其中设置了一个断点,但是当我开始运行应用程序时断点似乎没有启用,并且从未被命中,我理解这意味着 DLL包含该行的内容未加载。当实例化 DLL 中的某个类型的对象时,这很可能会导致引发异常 - 找不到对象的构造函数的主体。

我过去在一个不同的项目上成功地做到了这一点,所以我从该应用程序中提取了一个不同的 WinForms 用户控件,并在 XAML 中对其进行了实例化,一切正常。

所以它在这个 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>
4

6 回答 6

10

我遇到过这样的问题(但不是完全相同的错误消息)。似乎 WPF 无法实例化您的 Winforms 用户控件。

挑战在于找出原因。以下是我的建议,您可以尝试:

  1. 检查您是否启用了非托管调试(在项目属性 -> 调试中)
  2. 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.
  3. Comment out code of your Winforms User Control step by step and try again.
  4. Use Gflags.exe to turn on Loader Snaps (cf. Debugging LoadLibrary Failures)
于 2008-10-23T09:14:00.310 回答
1

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.

Solution is to either avoid using boost::threads entirely or restrict its use to .cpp files in native code.

于 2009-10-27T17:39:38.400 回答
1

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

于 2011-05-19T13:07:56.580 回答
0

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!

于 2009-04-03T15:52:47.900 回答
0

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.

mike

于 2009-07-16T06:40:41.557 回答
0

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.

于 2013-04-09T20:58:18.653 回答