在我的项目中,我有播放视频的WPF应用程序(ContentControl),它从第三方项目接收视频 - 它们将控件作为Winform Controls返回。
我实现了具有特定WPF控件(MedialElement)的第三方项目。 由于项目是通过API,我必须返回win form控件和不一个wpf控件。
我是如何解决这个问题的? 我将WPF控件包装在ElementHost对象中:
MediaElement newMediaElement = new MediaElement();
newMediaElement.Source = new Uri(@SomePath");
ElementHost mediaElementWrapper = new ElementHost(); //Wrap the media element
mediaElementWrapper.Child = newMediaElement;
我将mediaElementWrapper返回给我的消费者。
所以现在在主WPF播放器中,我得到了这个ElementHost并执行以下操作:
ElementHost elementHost = (ElementHost)session.PlayerControl;//PlayerControl is winform control
var extractedMediaElement = elementHost.Child as MediaElement;
elementHost.Child = null;//Disconnect the control - otherwise we get exception
return extractedMediaElement;
一切正常!
我的问题是,为什么在我的WPF Play应用程序中我必须引用System.Windowns.Forms,为什么引用WindowsFormsIntegration和WindowsBase是不够的?
修改:
我看到继承性heirarchy(as documented): System.Windows.Forms.Control的 System.Windows.Forms.Integration.ElementHost
但是WindowsormIntergration应该是WPF和winform都知道的唯一程序集吗?为什么WPF应用程序需要知道整个System.Form
答案 0 :(得分:-1)
没有办法,你可以在WPF中使用Winform控件。
您应该使用ElementHost并添加对System.Windows.Forms程序集的引用。