我正在尝试下载图片,并在完成时触发事件。我用这个:
BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png"));
btest.ImageOpened += btest_ImageOpened;
void btest_ImageOpened(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
但是,ImageOpened
事件不会触发。如果我使用:
BitmapImage
image.Source = btest;
它开火了。除非将ImageOpened
发件人设置为图片的来源,为什么BitmapImage
事件无法触发?
答案 0 :(得分:5)
我自己想出来了。默认情况下,在必要时不会初始化BitmapImage。 BitmapImage CreateOptions
的默认值为BitmapCreateOptions.DelayCreation
。解决此问题所需的只是将CreateOptions
设置为BitmapCreateOptions.None
。
我的工作代码是:
BitmapImage btest = new BitmapImage(new Uri("http://www.google.com/images/srpr/logo4w.png"));
btest.CreateOptions = BitmapCreateOptions.None;
btest.ImageOpened += btest_ImageOpened;
void btest_ImageOpened(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
答案 1 :(得分:1)
(我将根据我们的讨论发布此内容 - 因为它有助于OP获得正确的解决方案)
我猜 - 它从未使用过 - 因此它永远不会加载或打开 - 只是一个 想但是我觉得有道理
答案 2 :(得分:1)
我的两分钱,也许它可以帮助其他人...在页面上放置一个图像控件并在xaml中连接任何事件和源都可以正常工作,并且事件会触发。但是,当我在代码中加载位图图像并设置图像控件的源时,虽然图像加载正常,但图像控件的事件不会触发。我尝试了上面提到的所有位图选项,但它们似乎都没有用。我最终处理了位图图像的ImageOpened事件而不是处理事件的图像控件。请注意,在此阶段图像控件尚未完全加载图像,因此您必须引用位图图像以获取详细信息,而不是图像控件。
答案 3 :(得分:0)
在我的Windows phone 8.0 Silverlight应用程序中,当我将创建选项设置为时,我得到事件ImageOpened
BitmapCreateOptions.BackgroundCreation
。
设置为
时我没有得到它 BitmapCreateOptions.DelayCreation
(这是默认值)
或BitmapCreateOptions.None