【问题标题】:Windows Phone Image BindingWindows Phone 图像绑定
【发布时间】:2013-02-12 23:24:53
【问题描述】:

我想使用 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>

图像未显示。

谁能看出我哪里出错了?

【问题讨论】:

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


    【解决方案1】:

    如果图片不显示 - 检查路径; 例如我的绑定代码

     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"  />
    

    【讨论】:

    • 对此解决方案的小记...如果您稍微更改一下xaml,则不需要lowprofileimageloader!使用: 这将导致图像如果手机缓存中尚不可用,则在后台线程上下载。 (参见 MSDN 上的参考资料here)
    【解决方案2】:

    而不是做

    DataContext = _roomView;
    

    我做到了:

    Mylist.ItemsSource = _roomView.Room.Items;
    

    在 XML 中:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2013-09-23
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2014-01-15
      相关资源
      最近更新 更多