WPF在windows窗体上运行命令
我有一个WPF项目,其中添加了一个Windows窗体 windows窗体有一个方法WPF在windows窗体上运行命令,wpf,vb.net,winforms,Wpf,Vb.net,Winforms,我有一个WPF项目,其中添加了一个Windows窗体 windows窗体有一个方法runme()。但我想从WPF表格中调用它 目前我使用 Dim wpfForm as new wpfForm wpfForm.Show() 要从Windows窗体显示WPF窗体,请执行以下操作 但我不知道如何发回命令 我试过: Me.parent.runme() 但是这给了我错误我能想到的唯一方法就是为Winform创建自定义构造函数,并将对Wpf窗口的引用传递给它。将WPF窗口指定为Winforms窗口的所有
runme()
。但我想从WPF表格中调用它
目前我使用
Dim wpfForm as new wpfForm
wpfForm.Show()
要从Windows窗体显示WPF窗体,请执行以下操作
但我不知道如何发回命令
我试过:
Me.parent.runme()
但是这给了我错误我能想到的唯一方法就是为Winform创建自定义构造函数,并将对Wpf窗口的引用传递给它。将WPF窗口指定为Winforms窗口的所有者/父级 Winforms表单
Public Class Form1
Dim myParent As System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(parent As System.Windows.Window)
myParent = parent
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CType(myParent, MainWindow).runMe()
End Sub
End Class
Class MainWindow
Dim winForm As Form1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
winForm = New Form1(Me)
winForm.Show()
End Sub
Public Sub runMe()
Me.Background = Brushes.Red
End Sub
End Class
Wpf表格
Public Class Form1
Dim myParent As System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(parent As System.Windows.Window)
myParent = parent
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CType(myParent, MainWindow).runMe()
End Sub
End Class
Class MainWindow
Dim winForm As Form1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
winForm = New Form1(Me)
winForm.Show()
End Sub
Public Sub runMe()
Me.Background = Brushes.Red
End Sub
End Class
最终,有效的方法是按照Cyborgx37的建议创建一个接口
Interface ILogOut
Sub DoIt()
End Interface
Class WinFormWindow : Implements ILogOut
Public Sub DoIt() Implements ILogOut.DoIt
MsgBox("Message Received!")
End Sub
End Class
然后可以使用ILogOut接口从WPF窗口向Windows窗体窗口发送消息是否可以将构造函数参数添加到WPF窗体?您可以使用提供对父对象的引用。您甚至可以使用接口,而不是强制WPF表单接受WinForm表单作为参数。我得到的错误是'System.Windows.Window'类型的值不能转换为'WPFAApplication1.MainWindow'。这将在CType(..)上产生与以前相同的错误<代码>类型为“System.Windows.Window”的值不能转换为“WpfApplication1.MainWindow”。'以及
'WpfApplication1.Main'在实例化WPF时不能转换为“System.Windows.Window”
Window@Akshat这对我来说没有错误。确保使用WPF Windows类的名称。另外,您是从Wpf窗口创建Winforms窗口,还是从Winforms窗口创建Wpf窗口,这也有点令人困惑。如果是后者,则有一些方法将起作用。