-3

I have the following code in my WPF project:

    Dim ce As Windows.UI.Xaml.Controls.CaptureElement = New Windows.UI.Xaml.Controls.CaptureElement
    ce.Width = 300
    ce.Height = 200

    Dim elementHostPartial = New ElementHost
    elementHostPartial.Child = ce 'error here - cannot be converted to UIElement

    myWindowsFormsHost.Child = ce 'error here - cannot be converted to UIElement

    Dim captureManager = New MediaCapture
    Await captureManager.InitializeAsync

    ce.Source = captureManager
    Await captureManager.StartPreviewAsync

I'm sure that I'm missing something simple here. Can you see the resolution?

EDIT: This is a Windows 10 project. Two NuGet projects are included: Microsoft.Toolkit.Wpf.UI.Controls and Microsoft.Toolkit.Wpf.UI.XamlHost. The XAML has the following tag:

<WindowsFormsHost x:Name="myWindowsFormsHost" Width="150" Height="150" />

(although I'm open to switching to using a MediaElement or MediaPlayerElement) The framework is:

enter image description here

swabygw
  • 813
  • 1
  • 10
  • 22
  • Have you been following this: [Call Windows Runtime APIs in desktop apps](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance) and this: [Tutorial: Modernize a WPF app](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-wpf-tutorial) (and friends *on the left*) and maybe this: [Using the Visual layer in desktop apps](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/visual-layer-in-desktop-apps) – Jimi Sep 24 '20 at 21:08
  • No - they seem interesting, however. I didn't see any of them providing an answer to the question. Did I miss something? – swabygw Sep 24 '20 at 23:00
  • Btw, my code is an adaptation of this: https://social.msdn.microsoft.com/Forums/en-US/27da5b63-142f-4289-a330-1d722310684e/how-to-use-microsoftmediacapture-from-a-net-framework-application – swabygw Sep 24 '20 at 23:06
  • You're missing a description of how you setup your WPF app to handle and use UWP APIs. What .Net version you're using, on what System and what version of it this app is tested etc. – Jimi Sep 24 '20 at 23:07
  • Got it - updated. I'm running this in VS2019 Community. – swabygw Sep 24 '20 at 23:18
  • The `Windows.UI.Xaml.Controls` namespace is for UWP programs. The `ElementHost` is for hosting **WPF** objects in a **Windows Forms** program. What makes you think you would be able to use a UWP control in a WPF program using the WPF-hosting control? – Peter Duniho Sep 24 '20 at 23:34
  • From this article: https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/xaml-islands – swabygw Sep 24 '20 at 23:52
  • 1
    You need this: [WindowsXamlHost control for Windows Forms and WPF](https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost). Read what's in the first two links in the first comment for starters. – Jimi Sep 25 '20 at 00:17
  • Already have it...notice the "Windows.UI.Xaml.Controls.CaptureElement" mentioned in the code. – swabygw Sep 25 '20 at 00:53
  • That's a Control, which has nothing to do with anything already said and linked. – Jimi Sep 25 '20 at 04:12
  • This discussion is circular - will find another solution - thanks, anyway. – swabygw Sep 25 '20 at 04:30
  • Neither ElementHost nor WindowsFormsHost is what you actually need, i.e. a WindowsXamlHost. This needs more focus. – Clemens Sep 25 '20 at 06:49

1 Answers1

0

You could use the WindowsXamlHost control to host UWP controls in your WPF application assuming you target .NET Core 3 on Windows 10, version 1903 or later:

xmlns:xaml="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"
...
<xaml:WindowsXamlHost />

A WindowsFormsHost is used to host WinForms controls.

Please refer to the docs for details.

mm8
  • 163,881
  • 10
  • 57
  • 88