KinectStatus已连接断开连接未显示不知道为什么

本文关键字:连接 不知道 为什么 显示 断开 KinectStatus | 更新日期: 2023-09-27 18:01:14

我想显示kinect状态Connected或disconnect以及Device Connection ID。正在显示连接ID,但文本块中未显示状态我的代码是-

mainwindow.xaml.cs-

public partial class MainWindow : Window
{
    KinectSensor sensor;
    private MainWindowViewModel viewModel;
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
        this.viewModel = new MainWindowViewModel();
        this.DataContext = this.viewModel;
    }

    void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case KinectStatus.Connected:
                txtBlckStatus.Text = "Connected";

                 break;
            case KinectStatus.Disconnected:
                 txtBlckStatus.Text = "Disconnected";
                 break;
        }
    }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {

        if (KinectSensor.KinectSensors.Count > 0)
        {
            this.sensor = KinectSensor.KinectSensors[0];
            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
            this.StartSensor();
            this.sensor.ColorStream.Enable();
            this.sensor.DepthStream.Enable();
            this.sensor.SkeletonStream.Enable();
        }
        else
        {
            MessageBox.Show("No Sensor Connected!!");
            this.Close();
        }
    }
    private void StartSensor()
    {
        if(this.sensor!=null && !this.sensor.IsRunning)
        {
            this.sensor.Start();
            SetKinectInfo();
        }
    }
    private void StopSensor()
    {
        if (this.sensor != null && !this.sensor.IsRunning)
        {
            this.sensor.Stop();
        }
    }
    private void SetKinectInfo()
    {
        if(this.sensor!=null)
        {
            this.viewModel.ConnectionID = this.sensor.DeviceConnectionId;
        }
    }
}

主窗口.xaml

<Window x:Class="KinectInfoBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="Status:"></TextBlock>
        <TextBlock Grid.Row="0" Grid.Column="1" x:Name="txtBlckStatus"></TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Connection ID"></TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding ConnectionID}"></TextBlock>
        <Button Grid.Row="2" Grid.ColumnSpan="2" Content="Stop"  Margin="179,81,179,42" x:Name="StopSensorButton"></Button>
        </Grid>
    </Grid>
</Window>

mainwindowviewmodel.cs显示正在更改的连接id

namespace KinectInfoBox
{
    public class MainWindowViewModel:INotifyPropertyChanged
    {
        private string _connectionIDValue;
        public string ConnectionID
        {
            get { return _connectionIDValue; }
            set 
            {
                if(this._connectionIDValue!=value)
                {
                    this._connectionIDValue = value;
                    this.OnNotifyPropertyChange("ConnectionID");
                }
            }
        }
        public void OnNotifyPropertyChange(string propertyName)
        {
            if(this.PropertyChanged!=null)
            {
                   this.PropertyChanged.Invoke(this,newPropertyChangedEventArgs(propertyName));
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
        }
}

KinectStatus已连接断开连接未显示不知道为什么

代码完全正确,实际上kinect最初与系统连接,因此没有状态更改,但当您拔下kinect时,状态更改甚至被触发,因此您将在文本块中看到断开连接的状态,当您再次连接时,连接的状态将显示在那里。