• image绑定bitmap( Binding Image.Source from download 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
    {
    public partial class UserControl1 : UserControl
    {
    Test t;

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

    public class Test { public string Image { set; get; } }
    }

    再来看看另一种情况,要绑定的image是下载下来的byte[],没有路径,这时候

     <Image Stretch="None" Source="{Binding imageSource}" x:Name="img"></Image>

    cs:

     public class book//定义一个book类,需要绑定imagesource用ImageSource类型
    {
    public string 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,事情就是这样。

     

  • 相关阅读:
    如何做兼容性测试
    兼顾pc和移动端的textarea字数监控的实现方法
    js监听audio播放完毕
    layer弹出层移动端组件
    moment.js(日期处理类库)的使用
    移动端跳转方案-解决误触
    js移动端回退监听 popstate
    js大数计算之展示
    js大数计算之计算
    封装jquery的ajax
  • 原文地址:https://www.cnblogs.com/shit/p/2244816.html
Copyright © 2020-2023  润新知