微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight image绑定bitmap( Binding Image.Source from downloaded memory)

首先 xaml前台image的source是用string表示的

如:<image source="1.jpg"/>

想当然地以为source="{Binding imagesource}",imagesource也是必须是string,结果闹了我一个下午。

给后来人留点脚印,想想前者探索的艰辛啊。。

首先看看这段代码
<UserControl
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="ListBoxSilde.UserControl1"> 
<Grid x:Name="LayoutRoot"> 
<Image Source="{Binding Image}" Stretch="None" x:Name="img"></Image>
 </Grid>
</UserControl>
cs部分:
using System.Windows.Controls;

namespace ListBoxSilde
{
publicpartialclass UserControl1 : UserControl
 {
 Test t;

public UserControl1()
 { 
InitializeComponent();
 t = new test() { Image = "1.jpg" };
 img.DataContext = t; 
}
 }

publicclass Test { publicstring Image { set; get; } } 
}
再来看看另一种情况,要绑定的image是下载下来的byte[],没有路径,这时候
 <Image Stretch="None" Source="{Binding imageSource}" x:Name="img"></Image>
cs:
publicclass book//定义一个book类,需要绑定imagesource用ImageSource类型
{
publicstring bookname { get; set; }
public ImageSource imagesource { get; set; }
 }
 
void GetFirstimageCompleted(object sender,GetFirstimageCompletedEventArgs e)
 { 
ms = new MemoryStream(e.Result);//byte[]转stream
 BitmapImage image = new BitmapImage();
 image.SetSource(ms);
 book b = new book();
 b.imagesource =image;
 img.DataContext=b;//绑定对象
}

很显然,image.source绑定对象可以是ImageSource和string,事情就是这样。
 
 
技术交流群:29609188

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐