Handling Kinect in Main Window and passing this reference to UserControls

核能气质少年 提交于 2019-12-24 12:23:22

问题


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.

If you prefer, here are the github link.


回答1:


From your example code,

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);
}

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.

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

public partial class KinectUserControl : KinectControl
{
  ...
}

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.

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

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

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



来源:https://stackoverflow.com/questions/12774456/handling-kinect-in-main-window-and-passing-this-reference-to-usercontrols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!