Value of type 'CaptureElement' cannot be converted to Control or UIElement

484 Views Asked by At

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

1

There are 1 best solutions below

1
On

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.