2

在 XAML 中为 Image 设置 ImageSource 的一种方法是:

<Image Width="75" Height="75">
    <Image.Source>
        <BitmapImage UriSource={Binding Uri} DecodePixelWidth="75" DecodePixelHeight="75" />
    <Image.Source>
</Image>

此代码包含一个很好的优化,因为一个可能很大的位图将被解码为 75x75 像素。

我希望能够用我的自定义类替换 BitmapImage,如下所示:

<Image Width="75" Height="75">
    <Image.Source>
        <custom:PictureBitmap Picture={Binding Picture} Width="75" Height="75" />
    <Image.Source>
</Image>

我的应用程序实现了映射到数据库表的 Picture 类。图片类具有创建 BitmapImage 实例所需的一切。所以PictureBitmap本质上是BitmapImage的一个适配器。

我是这样开始的:

public class PictureBitmap : BitmapSource
{
    // TODO: create Picture Dependency Property
    // TODO: create a BitmapImage from Picture
    // TODO: implement abstract methods by delegating calls to BitmapImage
}

虽然 BitmapSource 是抽象的,但API 参考并没有说明如何实现它。

有谁知道如何将自定义对象提供给 Image.Source?

我的应用支持 Windows Phone Mango (7.5) 及更高版本。

谢谢!

4

1 回答 1

0

通过采取附加属性的方法解决了这个问题。

要使用我的自定义逻辑在图像上设置 Source 属性,我执行以下操作:

<Image Width="75" Height="75" my-namespace:PictureBitmap.Source={Binding Picture} />

这个链接非常有帮助:https ://stackoverflow.com/a/16103494/1809457

另外,我注意到 DecodePixelWidth 和 DecodePixelHeight 属性在 Mango 中不可用。

Mango 提供了 PictureDecoder 类,但缺点是必须在 UI 线程上使用。

于 2014-01-11T20:11:23.207 回答