【问题标题】:binding image in windows phone 8windows phone 8中的绑定图像
【发布时间】:2013-09-23 14:26:30
【问题描述】:

我可以将图像绑定到 xaml 而不需要使用它所在的图像源(主要方法)(在 xaml 页面中):

<Image Height="170" Width="220" Source="{Binding Source=Img}" ></Image>

因为我有从字节数组转换的图像并且没有任何名称或来源。 喜欢这里:

Image Img = new Image();
Img=ByteArraytoBitmap(ob0[0].Img.ToArray());

public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
    MemoryStream stream = new MemoryStream(byteArray);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
    return bitmapImage;
}

【问题讨论】:

    标签: image xaml windows-phone-7 binding windows-phone-8


    【解决方案1】:

    您可以在您的对象(ob0[0] 的类)上添加一个 BitmapImage 属性,然后可以将位图图像直接绑定到图像的源(注意绑定应该是 Source="{Binding Img}" 和不是Source="{Binding Source=Img}"。

    另一种解决方案是让您创建一个附加属性,这样应该可以工作:

    public class MyAttachedProperty
    {
        public static readonly DependencyProperty ByteArraySourceProperty =
            DependencyProperty.RegisterAttached("ByteArraySource", typeof (Byte[]), typeof (MyAttachedProperty), new PropertyMetadata(default(Byte[],byteArraySource)))
    
            private static void byteArraySource(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                Image img = d as Image;
                if (e.NewValue != null)
                {
                    img.Source = ByteArraytoBitmap((Byte[]) e.NewValue);
                }
            }
    
        public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(byteArray);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(stream);
            return bitmapImage;
        }
    
        public static void SetByteArraySource(UIElement element, Byte[] value)
        {
            element.SetValue(ByteArraySourceProperty, value);
        }
    
        public static Byte[] GetByteArraySource(UIElement element)
        {
            return (Byte[]) element.GetValue(ByteArraySourceProperty);
        }
    }
    

    然后要进行绑定,您可以像这样使用它:

    <Image Height="170" Width="220" local:MyAttachedProperty.ByteArraySource="{Binding Img}" ></Image>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2013-12-30
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      相关资源
      最近更新 更多