Windows Phone图像绑定

时间:2013-02-12 23:24:54

标签: silverlight binding windows-phone-7.1 windows-phone

我想使用Binding将图像放入ListBox。

下面是包含URI的对象:

_roomView.Room = new Room
        {
            Items = new List<Item> {
            new Item {ItemType = ItemType.BlueKey, ImageUri = "/Escape;component/Images/Items/a.jpg"},
            new Item {ItemType = ItemType.Bracelet, ImageUri = "/Escape;component/Images/Items/b.png"},
            new Item {ItemType = ItemType.Money, ImageUri = "/Escape;component/Images/Items/b.png"}}
        };
        DataContext = _roomView;

以下是XML:

  <ListBox x:Name="Mylist">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                        <Image Source="{Binding Room.Items.ImageUri}" Stretch="None" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

图像未显示。

谁能看到我哪里出错了?

2 个答案:

答案 0 :(得分:4)

如果图像没有显示 - 检查一下; 例如我的绑定代码

 public class Country
{       
    public String name
    {
        get;
        set;
    }       
    public String Flag
    {
        get
        {
            return "/Image/Countries/" + name + ".png";
        }
    }
}


<Image Width="100" Height="140" HorizontalAlignment="Left" Stretch="UniformToFill">
    <Image.Source>
       <BitmapImage UriSource="{Binding Flag}" CreateOptions="BackgroundCreation" />
    </Image.Source>
</Image>   

如果您正在使用项目中的图像,请确保构建内容并始终复制到输出目录。(图像的属性)

顺便说一下,如果你想从互联网上显示图像你需要使用LowProfileImageLoader 您可以阅读更多相关信息 http://blogs.msdn.com/b/delay/archive/2010/09/02/keep-a-low-profile-lowprofileimageloader-helps-the-windows-phone-7-ui-thread-stay-responsive-by-loading-images-in-the-background.aspx

 <Image  delay:LowProfileImageLoader.UriSource= "{Binding Flag}" HorizontalAlignment="Left" Stretch="Fill"   VerticalAlignment="Center" Width="24" Height="16" Margin="0,0,10,0"  />

答案 1 :(得分:0)

而不是做

DataContext = _roomView;

我做了:

Mylist.ItemsSource = _roomView.Room.Items;

并在XML中:

<Image Source="{Binding ImageUri}" Stretch="None" />

上面显示“ImageUri”而不是Room.Items.ImageUri,因为我已经传入了Rooom.Items。

相关问题