分享

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

 昵称10504424 2013-02-19
  1. 首先 xaml前台image的source是用string表示的  
  2.   
  3. 如:<image source="1.jpg"/>  
  4.   
  5. 想当然地以为source="{Binding imagesource}",imagesource也是必须是string,结果闹了我一个下午。  
  6.   
  7. 给后来人留点脚印,想想前者探索的艰辛啊。。  
  8.   
  9. 首先看看这段代码  
  10. <UserControl  
  11.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  12.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  13.  x:Class="ListBoxSilde.UserControl1">   
  14. <Grid x:Name="LayoutRoot">   
  15. <Image Source="{Binding Image}" Stretch="None" x:Name="img"></Image>  
  16.  </Grid>  
  17. </UserControl>  
  18. cs部分:  
  19. using System.Windows.Controls;  
  20.   
  21. namespace ListBoxSilde  
  22. {  
  23. publicpartialclass UserControl1 : UserControl  
  24.  {  
  25.  Test t;  
  26.   
  27. public UserControl1()  
  28.  {   
  29. InitializeComponent();  
  30.  t = new Test() { Image = "1.jpg" };  
  31.  img.DataContext = t;   
  32. }  
  33.  }  
  34.   
  35. publicclass Test { publicstring Image { setget; } }   
  36. }  
  37. 再来看看另一种情况,要绑定的image是下载下来的byte[],没有路径,这时候  
  38.  <Image Stretch="None" Source="{Binding imageSource}" x:Name="img"></Image>  
  39. cs:  
  40. publicclass book//定义一个book类,需要绑定imagesource用ImageSource类型   
  41. {  
  42. publicstring bookname { getset; }  
  43. public ImageSource imagesource { getset; }  
  44.  }  
  45.    
  46. void GetFirstImageCompleted(object sender,GetFirstImageCompletedEventArgs e)  
  47.  {   
  48. ms = new MemoryStream(e.Result);//byte[]转stream   
  49.  BitmapImage image = new BitmapImage();  
  50.  image.SetSource(ms);  
  51.  book b = new book();  
  52.  b.imagesource =image;  
  53.  img.DataContext=b;//绑定对象   
  54. }  
  55.   
  56. 很显然,image.source绑定对象可以是ImageSource和string,事情就是这样。  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多