在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;
}

在SIlverlight 4.0下更新WPF图像问题

尝试更改Load:的过载


this._myDomainContext.Load(screen, LoadBehavior.RefreshCurrent, true).Completed+= ...

我不完全确定问题的原因,但我在代码中有一些关于使用BitmapImage的指针。

  1. 在为image1.Source属性分配实际源之前,无需将其设置为null
  2. 不要处理传递给BitmapImage的流,以防控件试图从中读取
  3. 我个人在使用BitmapImage时使用了BeginInit(...)StreamSourceEndInit(...)