简体   繁体   English

从单独的WPF应用程序处理WinForms按钮的Click事件

[英]Handling a WinForms button's Click event from a separate WPF application

Summary: So I have two applications: one is WinForms and the other is WPF. 摘要:因此,我有两个应用程序:一个是WinForms,另一个是WPF。 I am trying to figure out how to click a button in the WinForms app and have the WPF app respond to that click. 我试图弄清楚如何单击WinForms应用程序中的按钮,并使WPF应用程序响应该单击。

So to get more detailed, I am working on an existing WinForms application, adding new features. 因此,为了更加详细,我正在研究现有的WinForms应用程序,并添加了新功能。 For one of those features, I needed to create a separate WPF application with an InkCanvas that sits on top of the WinForms application and allows the user to draw over the WinForms app. 对于这些功能之一,我需要使用InkCanvas创建一个单独的WPF应用程序,该应用程序位于WinForms应用程序的顶部,并允许用户绘制WinForms应用程序。

I have 3 buttons on the WinForms app that are meant to control the InkCanvas: one to enable drawing, one to enable erasing, and one to clear the canvas. 我在WinForms应用程序上有3个用于控制InkCanvas的按钮:一个用于启用绘图,一个用于启用擦除,一个用于清除画布。 What I need to figure out is how to handle those 3 buttons' Click events in the WPF application. 我需要弄清楚的是如何在WPF应用程序中处理这三个按钮的Click事件。

I've been looking at things like PRISM's EventAggregator (although I don't know if that would work between separate processes) and WCF. 我一直在研究像PRISM的EventAggregator(尽管我不知道在单独的流程之间是否可行)和WCF之类的东西。 WCF seems like what I should be using, but I have no experience using it, so it is slow going. WCF似乎应该使用,但是我没有使用它的经验,因此进展缓慢。 I have come across articles about WCF Duplex Services ( link )( link ) 我碰到过有关WCF双工服务的文章( link )( link

So I guess my question is: what is the best approach to handling a WinForms button's Click event in a separate WPF application? 所以我想我的问题是:在单独的WPF应用程序中处理WinForms按钮的Click事件的最佳方法是什么? Apologies in advance if this question has already been asked or if it is too vague. 如果已经问过这个问题或它太含糊,请提前道歉。

WinForms can host WPF elements using the ElementHost control, built into the Winforms control library. WinForms可以使用内置在Winforms控件库中的ElementHost控件来承载WPF元素。 If that doesn't work in this case (because you are using transparency), you can try using a new WPF window that you overlay on top of the application (see Open WPF window in WindowsForm APP ). 如果在这种情况下不起作用(因为正在使用透明性),则可以尝试使用一个新的WPF窗口,该窗口叠加在应用程序的顶部(请参阅WindowsForm APP中的“打开WPF”窗口 )。

This way you can wire up event handlers and communication without having to worry about IPC methods or managing application lifetimes between two different executables. 这样,您可以连接事件处理程序和通信,而不必担心IPC方法或管理两个不同可执行文件之间的应用程序生存期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM