1

我如何才能访问 longlistselector 项目。

我的 XAML 是:

<Grid x:Name="ContentPanel" Grid.Row="1" HorizontalAlignment="Left" Height="Auto" Margin="30,20,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="420">
  <phone:LongListSelector Name="longList" HorizontalAlignment="Left" Height="Auto" Margin="0" VerticalAlignment="Top" Width="420">
    <phone:LongListSelector.ItemTemplate>
      <DataTemplate>
        <Grid Background="Transparent">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="210" />
            <ColumnDefinition Width="210" />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="160" />
          </Grid.RowDefinitions>
          <Image Name="first" Width="210" Margin="0" Source="{Binding ImageName}" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" />
          <Image Name="second" Width="210" Margin="0" Source="{Binding ImageName}" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Right" />
        </Grid>
      </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
  </phone:LongListSelector>
</Grid>

我想首先第二次访问图像对象

我需要从包含ImageName字符串作为变量(保存 jpg 图像的路径)的类列表中连续分配这两个图像对象的源,然后将下两个图像对象分配到下一行,依此类推..

4

1 回答 1

0

将新文件夹添加到解决方案并将文件夹重命名为“模型”

然后添加一个名为“ImageSourceClass”的新类

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestApp.Model
{
    public class ImageSourceClass
    {
        public string ImageUrl { get; set; }

        public ImageSourceClass(string imageUrl)
        {
            this.ImageUrl = imageUrl;
        }
    }

    public class ImageData : ObservableCollection<ImageSourceClass>
    {
        public ImageData()
        {
            Add(new ImageSourceClass("/Assets/Images/Fruits.jpg"));
            Add(new ImageSourceClass("/Assets/Images/Beignets.jpg"));
            Add(new ImageSourceClass("/Assets/Images/Dessert.jpg"));
            Add(new ImageSourceClass("/Assets/Images/Pretzel.jpg"));
            Add(new ImageSourceClass("/Assets/Images/Shrimp.jpg"));
            Add(new ImageSourceClass("/Assets/Images/SteakSandwich.jpg"));
        }
    }
}

主页.xaml

将以下命名空间添加到 MainPage.xaml

注意:[根据您的解决方案名称命名空间可能会有所不同]

xmlns:MyModel="clr-namespace:TestApp.Model"

<Grid x:Name="LayoutRoot" Background="Transparent" >
<Grid.Resources>
      <MyModel:ImageData x:Key="imageDatasource"></MyModel:ImageData>
</Grid.Resources>
 <phone:LongListSelector x:Name="lstRestoItems" ItemsSource="{StaticResource imageDatasource}" LayoutMode="List">
     <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding ImageUrl}" Height="100" Width="100"         HorizontalAlignment="Left"></Image>
        </DataTemplate>
     </phone:LongListSelector.ItemTemplate>
 </phone:LongListSelector>

</Grid>

我们完成了,现在只需清理并重新构建您的应用程序并按 f5

于 2013-11-23T06:13:15.717 回答