using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.Kinect; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Resources; using System.Security.Policy; using System.Text; using System.Threading.Tasks; namespace KinectConnection { /// /// Manages Kinect events. /// public class KinectManager : ObservableObject { private KinectSensor kinectSensor; private bool status; private string statusText; /// /// Initializes a new instance of the KinectManager class. /// public KinectManager() { this.kinectSensor = KinectSensor.GetDefault(); this.Status = false; } /// /// Gets or sets a value indicating whether the Kinect is available. /// public bool Status { get { return status; } set { SetProperty(ref status, value); } } /// /// Gets or sets the status text of the Kinect. /// public string StatusText { get { return statusText; } set { SetProperty(ref statusText, value); } } /// /// Starts the Kinect sensor and subscribes to the IsAvailableChanged event. /// public void StartSensor() { this.kinectSensor.Open(); this.kinectSensor.IsAvailableChanged += this.KinectSensor_IsAvailableChanged; } /// /// Stops the Kinect sensor and unsubscribes from the IsAvailableChanged event. /// public void StopSensor() { this.kinectSensor.IsAvailableChanged -= this.KinectSensor_IsAvailableChanged; this.kinectSensor.Close(); } /// /// Updates the KinectManager properties based on the Kinect's availability. /// private void KinectSensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs args) { this.StatusText = this.kinectSensor.IsAvailable ? "Kinect Available" : "Kinect Not Available"; this.Status = this.kinectSensor.IsAvailable; // if sensor is unplugged => trigger the stop method if (!this.kinectSensor.IsAvailable) { this.StopSensor(); } // if its plugged back => trigger the start method else { this.StartSensor(); } } } }