KinectStatus-Connected Disconnected没有显示不知道为什么? [英] KinectStatus-Connected Disconnected not getting displayed dont know why?
问题描述
我想显示kinect状态 - 已连接或已断开连接以及设备连接ID。显示连接ID,但状态未显示在Textblock中我的代码是 -
mainwindow.xaml.cs-
公共部分类MainWindow:Window
{
KinectSensor传感器;
私人MainWindowViewModel viewModel;
public MainWindow()
{
InitializeComponent();
this.Loaded + = MainWindow_Loaded;
this.viewModel = new MainWindowViewModel();
this.DataContext = this.viewModel;
}
void KinectSensors_StatusChanged(对象发件人,StatusChangedEventArgs e)
{
开关(e.Status)
{
案例KinectStatus.Connected:
txtBlckStatus.Text =已连接;
休息;
案例K. inectStatus.Disconnected:
txtBlckStatus.Text =断开连接;
休息;
}
}
无效MainWindow_Loaded(对象发送者,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 SetKinect信息()
{
if(this.sensor!= null)
{
this.viewModel。 ConnectionID = this.sensor.DeviceConnectionId;
}
}
}
mainwindow.xaml
< window x:class =KinectInfoBox.MainWindowxmlns:x =#unknown>
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft。 com / winfx / 2006 / xaml
Title =MainWindowHeight =350Width =525>
< grid>
< grid>
< grid.rowdefinitions>
< rowdefinition>
< rowdefinition>
< rowdefinition>
< grid.columndefinitions>
< columndefinition> < columndefinition>
< textblock grid.row =0grid.column =0text =状态:>
< textblock grid.row =0 grid.column =1x:name =txtBlckStatus>
< textblock grid.row =1grid.column =0text =Connection ID>
< textblock grid.row =1grid.column =1text ={Binding ConnectionID}>
mainwindowviewmodel.cs - 显示更改的连接ID
命名空间KinectInfoBox
{
公共类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));
}
}
公共事件PropertyChangedEventHandler PropertyChanged;
}
}
I want to display the kinect status-Connected or disconnected and the Device Connection ID. Connection ID is getting displayed,but Status is not getting displayed in Textblock My code is-
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 SetKinectInfo()
{
if(this.sensor!=null)
{
this.viewModel.ConnectionID = this.sensor.DeviceConnectionId;
}
}
}
mainwindow.xaml
<window x:class="KinectInfoBox.MainWindow" xmlns:x="#unknown">
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>
<grid.columndefinitions>
<columndefinition> <columndefinition>
<textblock grid.row="0" grid.column="0" text="Status:">
<textblock grid.row="0" grid.column="1" x:name="txtBlckStatus">
<textblock grid.row="1" grid.column="0" text="Connection ID">
<textblock grid.row="1" grid.column="1" text="{Binding ConnectionID}">
mainwindowviewmodel.cs--to display the changing connection 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-Connected Disconnected没有显示不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!