在主窗口中处理Kinect并将此引用传递给UserControls [英] Handling Kinect in Main Window and passing this reference to UserControls

查看:90
本文介绍了在主窗口中处理Kinect并将此引用传递给UserControls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个带有许多UserControls的项目,某些用户控件使用Kinect KinectColorViewer.xaml 我想在主窗口中处理传感器的发现和设置(连接,断开等),并将其提供给我的UserControls,最好的方法是什么?

I have a project in WPF with a lot of UserControls, some user controls uses Kinect KinectColorViewer.xaml I want to handle the sensor discovering and setup (conect, disconect, etc) in main window and serve it to my UserControls, how is the best way to do it?

此处是解释我的问题的项目.

Here is the project that explains my question.

如果您愿意,此处是github链接.

If you prefer, here are the github link.

推荐答案

从您的示例代码中,

假设您希望维护来自Microsoft的尽可能多的可用代码,则将需要在初始化应用程序时建立对KinectSensorManager的引用.我的构造函数通常看起来像这样:

Assuming you want to maintain as much of the already available code from Microsoft, you will want to set up a reference to the KinectSensorManager on initializing your application. My constructor normally looks something like this:

private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();

public KinectSensorManager KinectSensorManager { get; private set; }

public MainViewModel()
{
    // initialize the Kinect sensor manager
    KinectSensorManager = new KinectSensorManager();
    KinectSensorManager.KinectSensorChanged += this.KinectSensorChanged;

    // locate an available sensor
    sensorChooser.Start();

    // bind chooser's sensor value to the local sensor manager
    var kinectSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
    BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
}

KinectSensorManager只是一个帮助程序类.您可以重写代码来轻松避免使用它,但是它并没有做任何不好的事情(对您而言有很多好处),因此我一直在使用它.另外,由于我假设您想重复使用尽可能多的代码,因此我们希望保持其使用率.

The KinectSensorManager is just a helper class. You can rewrite code to easily avoid using it, but it doesn't do anything bad (does a lot of nice stuff for you) so I've just keep using it. Also, since I'm assuming you want to re-use as much code as possible, we want to maintain its usage.

对于您的控件,您可以扩展KinectControl,它将为您设置一堆有用的项目.所以...

For your control, you can extend KinectControl which will set up a bunch of helpful items for you. So...

public partial class KinectUserControl : KinectControl
{
  ...
}

这将使您的控件可以访问许多可重写的函数,这些函数侦听各种事件(例如KinectSensorChanged).查看我们的KinectColorViewer代码,您可以看到它如何覆盖此功能,如果您交换Kinects,它可以自动开始显示新数据.

This will give your control access to a lot of override-able functions that listen in to various events (like KinectSensorChanged). Check our the KinectColorViewer code and you can see how it overrides this function, which allows it to automatically start displaying new data if you swap Kinects.

在XAML中声明控件时,您现在可以添加对KinectSensorManager的引用:

When declaring your control in the XAML you can now add a reference to the KinectSensorManager:

<my:KinectUserControl KinectSensorManager="{Binding KinectSensorManager}" />

因为您的控件现在具有KinectSensorManager属性,它也应该传递给KinectColorViewer控件.

Because your control now has a KinectSensorManager property, it should pass through to your KinectColorViewer control as well.

这篇关于在主窗口中处理Kinect并将此引用传递给UserControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆