我想显示kinect状态 - 已连接或已断开连接以及设备连接ID。 显示连接ID,但状态未显示在Textblock中 我的代码是 -
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;
}
}
}
mainwindow.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;
}
}
答案 0 :(得分:0)
代码完全没问题,实际上kinect最初是与系统连接的,所以没有状态改变,但当你拔掉kinect然后触发状态改变,所以你会看到文本块中的状态断开当你再次连接ity时,连接状态将显示在那里。