2013-05-03 89 views
2

試着學習Win Phone 8,以下是一個在線教程。在教程中,這個人使用ListBox來顯示文件,這對我來說工作正常。將文件綁定到longlistselector

但是,我認爲我們應該使用LongListSelector,所以我補充說,但沒有出現。

如果我把LongListSelector放在標記中,那麼當我在模擬器中運行應用程序時都不會顯示,所以我認爲我會通過綁定LongListSelector來獲取異常。我不明白爲什麼。

這很簡單,點擊一個按鈕,讀取目錄中的文件,顯示它們。

 <StackPanel x:Name="ContentPanel" Margin="12,0,12,0" Grid.Row="1" > 
     <Button Content="Show files" Click="Button_Click_1"/> 

     <ListBox x:Name="lb"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding Name}" /> 
         <Image x:Name="img" Source="{Binding Path}" Width="100" Height="100"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

     <phone:LongListSelector HorizontalAlignment="Left" 
           x:Name="llsFiles" 
           ItemTemplate="{StaticResource FilesDataTemplate}" 
           /> 

    </StackPanel> 

和LLS模板:

<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="FilesDataTemplate"> 
     <TextBlock Text="{Binding Name}"/> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources> 

然後隱藏代碼:

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     GetPackageFiles(); 
    } 

    private async Task GetPackageFiles() 
    { 
     //Get the folder where the app is installed on the phone. 
     var installFolder = Package.Current.InstalledLocation; 
     var imagesFolder = await installFolder.GetFolderAsync("Images"); 

     var fileList = await imagesFolder.GetFilesAsync(); 

     lb.ItemsSource = fileList; 

     llsFiles.ItemsSource = fileList.ToList(); 
    } 
+0

我覺得我一頭栽進這是怎麼回事結合,但不明白它。出於某種原因,取消Horizo​​ntalAlignment屬性會使列表呈現。不確定綁定是否發生,但是輸出完全被壓扁,或者實際上導致綁定失敗。 – 2013-05-15 15:29:36

回答

1

試試這個

//add this declaration 
List<FirstList> source = new List<FirstList>(); 
public class FirstList 
    { 
     [DataMember] 
     public string cItem { get; set; } 

     public FirstList(string item) 
     { 
      this.cItem = item; 
     } 
    } 

然後添加任何你只是這樣做。

source.Add(new FirstList(fileList.ToString()); 

讓你確認你已經將它

相關問題