我正在尝试在WPF应用程序中显示来自Kinect的摄像头。但是,图像显示为空白。
下面是我在Kinect类中的内容片段,这一切都正确触发,并且BitmapSource似乎创建得很好。
public delegate void FrameChangedDelegate(BitmapSource frame);
public event FrameChangedDelegate FrameChanged;
//this event is fired by the kinect service, and fires correctly
void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null)
{
return;
}
byte[] pixels = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(pixels);
int stride = colorFrame.Width * 4;
BitmapSource newBitmap = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
96, 96, PixelFormats.Bgr32, null, pixels, stride);
counter++;
//below is the call to the delegate
if ( FrameChanged != null)
{
FrameChanged(newBitmap);
}
}
}
以下是我在ViewModel中的内容。
void kinectService_FrameChanged(BitmapSource frame)
{
image = frame;
}
BitmapSource image;
public BitmapSource Image
{
get { return this.image; }
set
{
this.image = value;
this.OnPropertyChanged("Image");
}
}
以下是我在XAML View中的内容。
<Image Canvas.Left="212" Canvas.Top="58" Height="150" Name="image1" Stretch="Fill" Width="200" Source="{Binding Path=Image}"/>
所有事件和财产似乎都在更新。我做错了什么?
答案 0 :(得分:2)
image = frame;
应该是:
Image = frame;
否则您的财产更改通知将不会触发。
答案 1 :(得分:1)
尝试更改:
void kinectService_FrameChanged(BitmapSource frame)
{
this.image = frame;
}
到
void kinectService_FrameChanged(BitmapSource frame)
{
this.Image = frame;
}
因为您没有使用您的属性,所以永远不会调用PropertyChanged
事件,因此UI不会知道它需要获取新的图像值。
答案 2 :(得分:1)
我不确定,但你不需要使用大写我:
void kinectService_FrameChanged(BitmapSource frame)
{
Image = frame;
}
忘了添加:这就是WPF卡住的原因。所有这些小陷阱和陷阱。我很少在绑定应该阻止的应用程序中遇到“同步”问题,而现在我遇到了绑定本身的一些小问题。