C#学习教程:在wpfc#应用程序的另一个类中访问XAML按钮和复选框的值跑马灯的值我正在做一个WPFKinect项目。它是WindowsKinect开发工具包示例之一,称为“KinectExplorer”。您可以从KinectDeveloperToolkitSDKver1.5下载它。在kinectwindow.xaml中,我添加了一个按钮和一个复选框。此外,还有一个名为kinectskeleton.cs的类,我在其中创建了两个数据表和一个布尔变量。第一个DataTable在OnRender函数中填充,而另一个为空。布尔变量默认设置为false。所以,我想要的是当按下kinectwindow.xaml.cs中的按钮时,填充的DataTable的最新数据被复制到空的DataTable中。然后,当检查复选框时,布尔值将设置为true。那么,怎么做呢?我在类kinectskeleton.cs中定义了一个函数,它将数据从填充的数据表复制到空的数据表。在kinectwindow.xaml.cs中按钮的OnClick函数中,我从类kinectskeleton创建了一个对象并调用了这个函数,但是两个DataTables都是空的。CheckBox_Checked函数CheckBox_Checked:我将kinectskelton类的布尔值设置为true(在未选中的函数中,我将其设置为false)。然而,事实证明,在kinectskelton类中,它始终设置为默认值(false),并且永远不会进入我为其设置的if条件。希望现在更清楚了,等待任何建议。要下载该工具包,请点击此链接:http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx我的部分代码://-------------------------------------------------------------------------////版权所有(c)MicrosoftCorporation。版权所有。////------------------------------------------------------------------------------命名空间Microsoft.Samples.Kinect.KinectExplorer{使用System.Windows;使用System.Windows.Controls;使用System.Windows.Data;使用System.Windows。输入;使用Microsoft.Kinect;使用Microsoft.Samples.Kinect.WpfViewers;//////KinectWindow.xaml的交互逻辑。///publicpartialclassKinectWindow:Window{publicstaticreadonlyDependencyPropertyKinectSensorProperty=DependencyProperty.Register("KinectSensor",typeof(KinectSensor),typeof(KinectWindow),newPropertyMetadata(null));私人只读KinectWindowViewModel视图模型;//////初始化KinectWindow的新实例类,它提供对许多KinectSensor设置的访问///和输出可视化。///publicKinectWindow(){this.viewModel=newKinectWindowViewModel();//KinectSensorManager类是KinectSensor的包装器,它添加//状态逻辑和属性更改/绑定/等支持,并且是//KinectDiagnosticViewer的数据模型。this.viewModel.KinectSensorManager=newKinectSensorManager();绑定sensorBinding=newBinding("KinectSensor");sensorBinding.Source=这个;BindingOperations.SetBinding(this.viewModel.KinectSensorManager,KinectSensorManager.KinectSensorProperty,sensorBinding);//尝试为每个Kinect传感器打开骨架跟踪this.viewModel.KinectSensorManager.SkeletonStreamEnabled=true;this.DataContext=this.viewModel;初始化组件();}publicKinectSensorKinectSensor{get{return(KinectSensor)GetValue(KinectSensorProperty);}set{SetValue(KinectSensorProperty,value);}}公众号dStatusChanged(KinectStatus状态){this.viewModel.KinectSensorManager.KinectSensorStatus=status;}privatevoidSwap_Executed(objectsender,ExecutedRoutedEventArgse){GridcolorFrom=null;网格depthFrom=null;如果(this.MainViewerHost.Children.Contains(this.ColorVis)){colorFrom=this.MainViewerHost;depthFrom=this.Sid??eViewerHost;}else{colorFrom=this.Sid??eViewerHost;depthFrom=this.MainViewerHost;}colorFrom.Children.Remove(this.ColorVis);depthFrom.Children.Remove(this.DepthVis);colorFrom.Children.Insert(0,this.DepthVis);depthFrom.Children.Insert(0,this.ColorVis);}publicKinectSkeletonks=newKinectSkeleton();privatevoidCalibrate_Click(objectsender,RoutedEventArgse){ks.setcurrentdt();}privatevoidAngleDifference_Checked(objectsender,RoutedEventArgse){ks.is_checked=true;}privatevoidAngleDifference_Unchecked(objectsender,RoutedEventArgse){ks.is_checked=false;}}}//////一个ViewModelr一个Kinect窗口。///公共类KinectWindowViewModel:DependencyObject{publicstaticreadonlyDependencyPropertyKinectSensorManagerProperty=DependencyProperty.Register("KinectSensorManager",typeof(KinectSensorManager),typeof(KinectWindowViewModel),newPropertyMetadata(null));publicKinectSensorManagerKinectSensorManager{get{return(KinectSensorManager)GetValue(KinectSensorManagerProperty);}set{SetValue(KinectSensorManagerProperty,value);}}}}命名空间Microsoft.Samples.Kinect.WpfViewers{使用System.Collections.Generic;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Media;使用Microsoft.Kinect;使用系统数据;使用System.Windows.Media.Media3D;使用System.Globalization;//////此控件用于渲染玩家的骨架。///如果ClipToBounds设置为“false”,将允许透支///它的边界。///公共类KinectSkeleton:Control{publicboolis_ch埃克=假;publicDataTablex=newDataTable();公共数据表y=新数据表();protectedoverridevoidOnRender(DrawingContextdrawingContext){base.OnRender(drawingContext);varcurrentSkeleton=this.Skeleton;//如果我们没有骨架就不要渲染,或者没有跟踪它if(drawingContext==null||currentSkeleton==null||currentSkeleton.TrackingState==SkeletonTrackingState.NotTracked){return;}//在骨架离开屏幕的显示边缘附近显示渐变this.RenderClippedEdges(drawingContext);switch(currentSkeleton.TrackingState){caseSkeletonTrackingState.PositionOnly:if(this.ShowCenter){drawingContext.DrawEllipse(this.centerPointBrush,null,this.Center,BodyCenterThickness*this.ScaleFactor,BodyCenterThickness*this.ScaleFactor);}休息;caseSkeletonTrackingState.Tracked://这里我定义了数据表if(is_checked==false){//用值a填充数据表x}else{//用值b填充数据表x}break;}}publicvoidsetcurrentdt(){//用填充的“x”填充空数据表“y”y=x.Copy();}}}checkboxxaml代码:KinectExplorerFPSFPSViewModel类:publicclassViewModel{publicboolIsChecked{get;放;}publicboolis_clicked{得到;猜测,但在我看来你的问题是由于有两个不同的KinectSkeleton实例KinectSkeleton:如何解决这个问题?publicKinectSkeletonks=newKinectSkeleton();现在您将只有一个KinectSkeleton实例,因此OnRender将使用您从事件处理程序修改的相同数据XAML按钮和复选框值共享所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
