fork download
  1. /*
  2.  MainWindow.xaml
  3. <Window x:Class="PhotoViewer.MainWindow"
  4.   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.   Title="MainWindow" Height="211" Width="563">
  7.   <Grid>
  8.   <ListBox x:Name="PhotoListBox" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled">
  9.   <ListBox.ItemsPanel>
  10.   <ItemsPanelTemplate>
  11.   <StackPanel Orientation="Horizontal" Height="{Binding PhotoListBox.Height}" />
  12.   </ItemsPanelTemplate>
  13.   </ListBox.ItemsPanel>
  14.   <ListBox.ItemTemplate>
  15.   <DataTemplate>
  16.   <Grid Margin="20" >
  17.   <Grid.RowDefinitions>
  18.   <RowDefinition Height="*"/>
  19.   <RowDefinition Height="Auto"/>
  20.   </Grid.RowDefinitions>
  21.   <Image Source="{Binding FilePath}" HorizontalAlignment="Center" />
  22.   <TextBlock Grid.Row="1" Text="{Binding Caption}" HorizontalAlignment="Center"/>
  23.   </Grid>
  24.   </DataTemplate>
  25.   </ListBox.ItemTemplate>
  26.   </ListBox>
  27.   </Grid>
  28. </Window>
  29. */
  30.  
  31. using System;
  32. using System.Collections.ObjectModel;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Threading.Tasks;
  36. using System.Windows;
  37.  
  38. namespace PhotoViewer
  39. {
  40. /// <summary>
  41. /// MainWindow.xaml の相互作用ロジック
  42. /// </summary>
  43. public partial class MainWindow : Window
  44. {
  45. private ObservableCollection<PhotoInfo> photos = new ObservableCollection<PhotoInfo>();
  46.  
  47. public MainWindow()
  48. {
  49. InitializeComponent();
  50. this.Loaded += MainWindow_Loaded;
  51. this.PhotoListBox.ItemsSource = photos;
  52. }
  53.  
  54. private static bool IsImageFile(string fileName)
  55. {
  56. // 適当
  57. var ext = Path.GetExtension(fileName).ToLowerInvariant();
  58. return ext == ".png" || ext == ".jpg";
  59. }
  60.  
  61. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  62. {
  63. Task.Factory.StartNew(() =>
  64. {
  65. foreach (var path in Directory.EnumerateFiles(".").Where(IsImageFile))
  66. {
  67. Dispatcher.BeginInvoke(new Action(() =>
  68. {
  69. photos.Add(new PhotoInfo
  70. {
  71. FilePath = Path.GetFullPath(path)
  72. });
  73. }));
  74. }
  75. });
  76. }
  77. }
  78.  
  79. public class PhotoInfo
  80. {
  81. public string FilePath { get; set; }
  82.  
  83. public string Caption
  84. {
  85. get
  86. {
  87. return Path.GetFileName(FilePath);
  88. }
  89. }
  90. }
  91. }
  92.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty