从WPF打开Winform [英] Opening a Winform from WPF

查看:104
本文介绍了从WPF打开Winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢,

工具:Visual Studio 2010/2012

我已经在WPF中创建了一个图表作为用户控件,我正在Winform上使用此控件
作为使用ElementHost控件的托管元素.

我的问题是有什么办法可以从此WPF控件加载Winforms.请在
上说 用户控件的第一个链接(按钮)打开Form1,用户控件的第二个链接(按钮)打开Form2.
当我添加项目引用时,它会显示引用将导致循环依赖".


任何建议.

Thanks in Advance,

Tool: Visual Studio 2010/2012

I have created a Diagram in WPF as a User control, I am using this control on Winform
as a hosted element using ElementHost control.

My Question is there any way i can load my winforms from this WPF control.Let say on
First link (button) of User Control open Form1 and on second link (button) of User Control open Form2.
When I am adding Project reference then it says "Reference would cause a circular dependency".


Any advice.

推荐答案

问题与WPF和System.Windows.Forms之间的互操作性无关.这个问题很复杂,而循环依赖的问题也很简单.您应该只避免程序集之间的循环引用.您可以有另外两个引用的单独的程序集.由于您没有显示代码,所以所有人都能告诉您.

对于从WPF应用程序加载表单,这通常是不可能的.最好避免将WPF和Forms混合使用.确实存在互操作性,但是它旨在在WPF UI中承载Forms控件或在Forms UI中承载WPF控件:
http://msdn.microsoft.com/en-us/library/ms751761.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms742215.aspx [ ^ ].

您不能托管表单,但是可以执行以下操作:将表单的所有内容放入面板中并托管该面板;但最好设计一些控件(可能是UserControl)以更好地适合WPF设计.

在同一个进程的两个不同线程中运行两个不同的UI的可能性还很深奥,但鲜为人知,但这几乎不够好,因为两者之间的通信非常有限.

总体而言,最好不要在一个过程中混合使用WPF和Forms.

—SA
The problem is not related to the interoperability between WPF and System.Windows.Forms. This problem is very complex, and the problem with circular dependencies is very simple. You should just avoid circular references between assemblies. You can have a separate assembly referenced by other two. As you did not show your code, that''s all one can tell you.

As to the loading a form from WPF application, this is generally impossible. It''s the best to avoid mixing WPF and Forms. Interoperability does exist, but it is designed to host a Forms control in WPF UI or a WPF control in Forms UI:
http://msdn.microsoft.com/en-us/library/ms751761.aspx[^],
http://msdn.microsoft.com/en-us/library/ms742215.aspx[^].

You cannot host a form, but you can do the following: put all content of a form into a panel and host this panel; but better design some control (possibly UserControl) to better fit the WPF design.

There is also an esoteric and very little known possibility to run two different UIs in two different threads of the same process, but it hardly be good enough, because communication between the two would be extremely limited.

Overall, avoiding to mix WPF and Forms in one process would be the very best thing.

—SA


这篇关于从WPF打开Winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆