我几乎没有尝试过将 Cognex In-Sight SDK 与 WPF 应用程序一起使用。 SDK 中有一些示例,但不幸的是仅适用于 WinForms。我想知道我可以从 Cognex DLL 为 WinForm 控件制作一个主机,但没有成功。我添加了 WinFormsIntegration 和 System.Windows.Forms.dll。我在 XAML 中添加了一个命名空间:
xmlns:cognex="clr-namespace:Cognex.InSight.Controls.Display;assembly=Cognex.InSight.Controls.Display"
并尝试使用:
<WindowsFormsHost HorizontalAlignment="Left" Height="244" Margin="314,73,0,0" VerticalAlignment="Top" Width="275">
<cognex:CvsInSightDisplay Name="Display" />
</WindowsFormsHost>
但它抛出:名称空间中不存在该名称。奇怪,因为当输入完全相同的控件名称时建议。
还有其他选项可以在 WPF 中获取 Cognex 控件吗? OpenAI 建议使用名为 Cognex.InSight.Controls.Wpf.dll 的文件,但我找不到它。
问候。皮奥特。
我正在尝试做同样的事情。但我也遇到了一个例外。 我已经包含了所有应该包含的 DLL。 (还有一个可以运行的 WinForm 应用程序)
System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'Cognex.InSight.Controls.Display.CvsInSightDisplay' that matches the specified binding constraints threw an exception.
---> System.TypeInitializationException: The type initializer for 'Cognex.InSight.Controls.Display.CvsInSightDisplay' threw an exception.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Cognex.InSight.Controls.Display.CvsInSightDisplay..cctor()
--- End of inner exception stack trace ---
at Cognex.InSight.Controls.Display.CvsInSightDisplay..ctor()
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
--- End of inner exception stack trace ---
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
XAML
<UserControl x:Class="Camera.Controls.CvsInSightDisplayHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cognex="clr-namespace:Cognex.InSight.Controls.Display;assembly=Cognex.InSight.Controls.Display"
mc:Ignorable="d">
<DockPanel>
<WindowsFormsHost>
<cognex:CvsInSightDisplay />
</WindowsFormsHost>
</DockPanel>
</UserControl>