您的位置:首页 > 其它

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

2012-11-08 15:10 501 查看
首先xaml前台image的source是用string表示的

如:<imagesource="1.jpg"/>

想当然地以为source="{Bindingimagesource}",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">
<Gridx:Name="LayoutRoot">
<ImageSource="{BindingImage}"Stretch="None"x:Name="img"></Image>
</Grid>
</UserControl>
cs部分:
usingSystem.Windows.Controls;

namespaceListBoxSilde
{
publicpartialclassUserControl1:UserControl
{
Testt;

publicUserControl1()
{
InitializeComponent();
t=newTest(){Image="1.jpg"};
img.DataContext=t;
}
}

publicclassTest{publicstringImage{set;get;}}
}
再来看看另一种情况,要绑定的image是下载下来的byte[],没有路径,这时候
<ImageStretch="None"Source="{BindingimageSource}"x:Name="img"></Image>
cs:
publicclassbook//定义一个book类,需要绑定imagesource用ImageSource类型
{
publicstringbookname{get;set;}
publicImageSourceimagesource{get;set;}
}

voidGetFirstImageCompleted(objectsender,GetFirstImageCompletedEventArgse)
{
ms=newMemoryStream(e.Result);//byte[]转stream
BitmapImageimage=newBitmapImage();
image.SetSource(ms);
bookb=newbook();
b.imagesource=image;
img.DataContext=b;//绑定对象
}

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

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐