在SIlverlight 4.0下更新WPF图像问题
本文关键字:WPF 图像 问题 更新 0下 SIlverlight | 更新日期: 2023-09-27 18:20:05
当我的页面加载并按下button1
时,我可以获得图像并看到它。
但当我第二次点击它时,它根本不起作用。我调试了它button1_Click(...)
,我确信imageData != null
。
我真的不知道怎么回事。。。请帮帮我!
private void button1_Click(object button, RoutedEventArgs e)
{
Guid sid = Guid.Parse("087462df-e4b6-484c-879e-cccc37b4c1f4");
EntityQuery<Screenshot> screen = this._myDomainContext.GetScreenshotQuery(sid);
this._myDomainContext.Load(screen).Completed += (sender, args) =>
{
try
{
byte[] imageData = (((LoadOperation<Screenshot>)sender).Entities.FirstOrDefault()).Screen;
if (imageData != null)
{
BitmapImage img = Utilities.Graphics.GetImage(imageData);
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image1.Source = null;
image1.Source = img;
}
}
catch
{
}
};
}
和
public static BitmapImage GetImage(byte[] rawImageBytes)
{
BitmapImage imageSource = null;
try
{
using (MemoryStream stream = new MemoryStream(rawImageBytes))
{
stream.Seek(0, SeekOrigin.Begin);
BitmapImage b = new BitmapImage();
b.SetSource(stream);
imageSource = b;
}
}
catch
{
}
return imageSource;
}
尝试更改Load:的过载
this._myDomainContext.Load(screen, LoadBehavior.RefreshCurrent, true).Completed+= ...
我不完全确定问题的原因,但我在代码中有一些关于使用BitmapImage
的指针。
- 在为
image1.Source
属性分配实际源之前,无需将其设置为null
- 不要处理传递给
BitmapImage
的流,以防控件试图从中读取 - 我个人在使用
BitmapImage
时使用了BeginInit(...)
、StreamSource
和EndInit(...)