如何在不同的cs文件中的xaml文件上访问KinectSensorchooser对象?

时间:2013-06-28 15:26:47

标签: wpf kinect

我是WPF和c#的初学者。我正在尝试使用Kinect for Windows创建应用程序。我的Kinect.cs中有一个kinectsensorchooser,它控制MainWindow.xaml中的SensorChooser 但我不确定如何控制它 我的代码如下:

MainWindow.xaml

<Canvas>  
<k:KinectSensorChooserUI KinectSensorChooser="{Binding SCkinectSensorChooser} "Name="sensorChooserCP"/>  
<k:KinectUserViewer k:KinectRegion.KinectRegion="{Binding kinectRegionCP}"/>  
<k:KinectRegion Name="kinectRegionCP" KinectSensor="{Binding ElementName=SCkinectsensor}">  
<Grid>  
some kinect tile buttons come in here...  
</Grid>  
</k:KinectRegion>  
</Canvas>  

Kinect.cs

public KinectSensor SCkinectsensor;  
public KinectSensorChooser SCkinectSensorChooser;  

在程序执行期间将动态设置两个对象的值。我希望这些更改能够反映在MainWindow

App.xaml

<Application x:Class="Kinect.App"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
StartupUri="MainWindow.xaml"  
xmlns:local="clr-namespace:Kinect">  
<Application.Resources>  
<local:Kinect x:Key="Kinect" />  
</Application.Resources>

我做错了,代码没有按照我的意愿回应......我该怎么办?我需要帮助

1 个答案:

答案 0 :(得分:1)

您是在主窗口中设置DataContext吗?如果没有,您的Binding将无能为力。

<强> MainWindow.xaml

<Window x:Class="Kinect.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{StaticResource Kinect}">
...
</Window>

另一点需要注意的是,在WPF中,您只能绑定到属性

<强> Kinect.cs

public KinectSensor SCkinectsensor { get; private set; }
public KinectSensorChooser SCkinectSensorChooser { get; private set; }

如果您希望在构造函数之外更改这些属性,则此类需要实现INotifyPropertyChanged,您的属性将如下所示:

private KinectSensor kinectSensor;
public KinectSensor SCkinectSensor
{
    get { return kinectSensor; }
    set 
    {
        kinectSensor = value;
        PropertyChanged(this, new PropertyChangedEventArgs("SCkinectSensor");
    }
}