在listview metro应用程序上添加图像

时间:2013-06-14 17:13:32

标签: listview windows-8 windows-runtime microsoft-metro windows-store-apps

public class ImageChannel
    {
        public string ImagePath { get; set; }

        public ImageSource Image
        {
            get
            {
                return new BitmapImage(new Uri("ms-appx://" + this.ImagePath));
            }
        }

        public List<ImageChannel> ImageChannels
        {
            get
            {
                return new List<ImageChannel>() 
           {
              new ImageChannel() { ImagePath="/Assets/image.png" }};
                // the other images
            }

        }
    }

<ListView ItemsSource="{Binding ImageChannels}" Margin="222,10,340,140">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Image  Height="113" Source="{Binding}" Stretch="None"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

为什么这不起作用?我真的迷失在这里。我只是想在listview中添加的项目旁边添加一个图像,例如“Image”|“Microsoft”等等。谢谢!

2 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

首先,从...

更改Image属性的声明
public ImageSource Image

public BitmapImage Image

ImageChannels属性获取访问器的实现将在每次访问时创建列表。在ImageChannel类中声明和初始化List的成员列表并在get访问器中返回它会更有意义。

Image.Source的XAML代码中的绑定不完整。你需要说明你绑定它的内容。

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

如果你解决了这些问题,你应该更接近于让它发挥作用。

答案 1 :(得分:0)

首先,您的ImageChannel类应如下所示:

public class ImageChannel
{
   public string ImagePath { get; set; }

   public BitmapImage Image
   {
       get
       {
          return new BitmapImage(new Uri("ms-appx://" + this.ImagePath));
       }
   }
}

然后在你的代码中,创建一个ImageChannel对象列表,如你所愿:

List<ImageChannel> imageChannel = new List<ImageChannel>();
imageChannel.Add(new ImageChannel()
{
  ImagePath = "image path here"
});

然后将ImageChannel List对象分配给ListView的itemsSource属性

myListView.itemsSource = imageChannel;

xaml中的ListView控件应如下所示

<ListView x:Name="myListView" Margin="222,10,340,140">
      <ListView.ItemTemplate>
            <DataTemplate>
                 <Image  Height="113" Source="{Binding Image}" Stretch="None"/>
             </DataTemplate>
       </ListView.ItemTemplate>
</ListView>

这应该适合你。