gpt4 book ai didi

c# - WPF-VB6 Interop 导致计时器运行时错误

转载 作者:太空狗 更新时间:2023-10-30 01:36:34 25 4
gpt4 key购买 nike

我创建了以下测试场景来跟踪 VB6-WPF 互操作的错误:

有一个显示带有文本框的表单的 .Net WPF DLL。文本框不处理键盘输入——没有数据绑定(bind),没有事件处理程序。 DLL 是 COM 可见的。

VB6 .exe 调用 DLL ShowWindow 函数来显示 WPF 窗体。当我在文本框中键入任何键时,它会导致 VB 应用程序出现“运行时错误‘6’溢出”错误。错误发生在 Timer1 事件的“dVal = 0”行。

我猜错误的发生是因为未处理的键事件冒泡到 VB6 应用程序。 WPF 事件是否应该冒泡到 VB6?为什么问题会出现在计时器中?为什么只在设置双变量时才会发生?如果“dVal = 0”行不存在,则不会发生。有什么想法吗?

显示窗口的公共(public)类的 C# 代码:

public class NetFormIFace : INetFormIFace
{

public void ShowWindow1()
{
Window1 w1;
w1 = new Window1();
w1.Show();
}

}

窗口的 XAML。代码隐藏中没有添加任何内容:

<Window x:Class="SSEC.NetForm.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Canvas>
<TextBox Height="34" Canvas.Left="35" TextWrapping="Wrap" Text="TextBox" Canvas.Top="52" Width="94" />
</Canvas>
</Window>

VB6 代码:

Option Explicit

Dim IFace As NetFormIFace

Private Sub Command1_Click()
IFace.ShowWindow1
End Sub

Private Sub Form_Load()
Set IFace = New NetFormIFace
Timer1.Interval = 50
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Dim iVal As Integer
Dim dVal As Double

iVal = 0 ' Does not cause an error
dVal = 0 ' Causes an error
End Sub

最佳答案

您正在创建一个无模式 WPF 弹出窗口,它在非托管 VB6 应用程序的消息循环上运行。对此有一些规定:Sharing Message Loops Between Win32 and WPF .我不确定这对您的特定情况是否有帮助,即使您设法在 VB6 端实现了消息循环要求。

理想情况下,您应该在 VB6 端创建一个非托管 Win32 弹出窗口,并使用 HwndSource在该窗口中承载您的 WPF 内容。检查Walkthrough: Hosting WPF Content in Win32 .

或者,您可以尝试将 WPF 复合控件公开为 VB6 的 ActiveX 控件。首先,您需要确保它在 VB.NET WinForms 中工作 UserControl , 检查 Walkthrough: Hosting a WPF Composite Control in Windows Forms .然后,使用 Microsoft InteropForms Toolkit 将 WinForms UserControl 作为 ActiveX 控件公开给 VB6 .我可能会走那条路。

关于c# - WPF-VB6 Interop 导致计时器运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com