gpt4 book ai didi

c# - 使用 WindowsXamlHost Overlay 托管 CaptureElement

转载 作者:行者123 更新时间:2023-12-05 05:07:06 26 4
gpt4 key购买 nike

我试图在我的 WPF .NET Core 3.1 应用程序中用 WindowsXamlHost 托管的 CaptureElement(Camera Feed)上覆盖一个 Grid,但 CaptureElement 始终位于任何其他控件之上。

是否无法在 WindowsXamlHost/CaptureElement 之上覆盖网格?

¦ 已创建示例应用程序 https://github.com/ValonK/SampleWPFXamlHost .我需要 CaptureElement,但还没有找到任何东西来捕捉相机源并在它们上面放置一些叠加层。

Xaml 代码

<Window x:Class="SampleWPFXamlHost.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SampleWPFXamlHost"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid Height="200"
Width="200"
Background="Red"/>
<ContentControl HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Content="{Binding XamlHostCaptureElement}"/>


</Grid>
</Window>

View 模型

public class MainViewModel : PropertyChangedAware
{
public MainViewModel()
{
GetUwpCaptureElement();
}

private MediaCapture _mediaCapture;

public MediaCapture MediaCapture {
get {
if (_mediaCapture == null)
_mediaCapture = new MediaCapture();
return _mediaCapture;
}
set {
_mediaCapture = value;
OnPropertyChanged(nameof(MediaCapture));
}
}


public CaptureElement CaptureElement { get; set; }

public WindowsXamlHost XamlHostCaptureElement { get; set; }

/// <summary>
/// Create / Host UWP CaptureElement
/// </summary>
private void GetUwpCaptureElement()
{
XamlHostCaptureElement = new WindowsXamlHost
{
InitialTypeName = "Windows.UI.Xaml.Controls.CaptureElement"
};
XamlHostCaptureElement.ChildChanged += XamlHost_ChildChangedAsync;
}

private async void XamlHost_ChildChangedAsync(object sender, EventArgs e)
{
var windowsXamlHost = (WindowsXamlHost)sender;

var captureElement = (CaptureElement)windowsXamlHost.Child;
CaptureElement = captureElement;
CaptureElement.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill;

try
{
await StartPreviewAsync();
}
catch (Exception)
{

}
}

private async Task StartPreviewAsync()
{
try
{
await MediaCapture.InitializeAsync();
}
catch (UnauthorizedAccessException ex)
{
//_logger.Info($"The app was denied access to the camera \n {ex}");
return;
}

try
{
CaptureElement.Source = MediaCapture;
await MediaCapture.StartPreviewAsync();
}
catch (System.IO.FileLoadException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}

}
}

public class PropertyChangedAware : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

最佳答案

Is it not possible to overlay a grid on top of WindowsXamlHost/CaptureElement?

没有。就像 WindowsFormsHost 中的 Windows 窗体控件一样,WindowsXamlHost 托管在始终绘制在 WPF 元素之上的单独 HWND 中。

来自docs :

A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of WPF elements.

有一个类似的问题有一些可用的答案here .

关于c# - 使用 WindowsXamlHost Overlay 托管 CaptureElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59416432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com