将图像源绑定到BitmapSource时,图像保持空白
我试图在WPF应用程序中显示来自Kinect的摄像头源。但是,图像显示为空白。将图像源绑定到BitmapSource时,图像保持空白
下面是我在我的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视图。
<Image Canvas.Left="212" Canvas.Top="58" Height="150" Name="image1" Stretch="Fill" Width="200" Source="{Binding Path=Image}"/>
所有的事件和属性似乎都被更新。我究竟做错了什么?
image = frame;
应该是:
Image = frame;
否则你的属性更改通知,将不会触发。
哦,亲爱的。我无法相信我从未发现过这一点。非常感谢! – benjgorman 2012-03-07 17:31:20
尝试改变:
void kinectService_FrameChanged(BitmapSource frame)
{
this.image = frame;
}
到
void kinectService_FrameChanged(BitmapSource frame)
{
this.Image = frame;
}
因为你不使用你的财产,该PropertyChanged
事件将永远不会被调用,所以UI不会知道它需要获得新的图像价值。
我不知道,但并不需要使用大写字母I:
void kinectService_FrameChanged(BitmapSource frame)
{
Image = frame;
}
忘了补充:这就是为什么WPF stucks。所有这些小陷阱和陷阱。我很少在应用程序中出现“同步”问题,因为绑定本应阻止,现在我却遇到了绑定本身的一些小问题。
完全同意,主代码全部正常工作,并且您遇到了一个您不会注意的小问题。主要是因为你认为它必须是一个复杂的问题。 – benjgorman 2012-03-07 17:37:35
尝试FrameChanged:this.Image = frame – 2012-03-07 17:29:18