0
votes

In my project I have WPF Application that play videos (ContentControl), It receive the videos from third party projects - they return the control as Winform Controls.

I Implemented a 3rd party project that has a certain WPF control (MedialElement). Since the project is via API, I must return win form control and not a wpf control.

How i solved this issue ?
I wrapped the WPF control inside an ElementHost object:

MediaElement newMediaElement = new MediaElement();
newMediaElement.Source = new Uri(@SomePath");
ElementHost mediaElementWrapper = new ElementHost(); //Wrap the media element
mediaElementWrapper.Child = newMediaElement;

And i return the mediaElementWrapper to my consumer.

So now in the main WPF player, I get this ElementHost and do the following:

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;

Everything is working fine !

My question is, Why in my WPF Play application i must reference System.Windowns.Forms, Why referece for WindowsFormsIntegration and WindowsBase is not enough ?

EDIT:

I see the inheritrance heirarchy (as documented): System.Windows.Forms.Control System.Windows.Forms.Integration.ElementHost

But does the WindowsormIntergration should be the only assembly that both WPF and winform know ? why does the WPF application needs to know the entire System.Form

1
It needs to have a reference to that assembly exactly because ElementHost derives from Control. Otherwise how could the compiler know e.g. which methods are legal to call on an instance and what their signatures are? And how could the runtime actually make the call? - Jon
Why there is no seperation to lower-level objects? e.g i one project of mine, i took the IntPtr of the window handler (of the winform control), sent the pointer to another process, and created a HwndHost that direct to that handler. This way the player does not need to know win forms - ilansch
I can't parse that comment at all. The situation is rather simple: if you use a type, you need to reference all assemblies in which its bases are defined. That's all. - Jon
I will show everyone later on how this is done. Specially to the one who -1 my question. I should have passed IntPtr to the Win32 windows handler, then in WPF application I can take over the windows using HwndSource that points to that IntPtr - ilansch

1 Answers

-1
votes

There is no way, you can use Winform Controls in WPF.

you should use ElementHost and add reference to System.Windows.Forms assembly.