私のアプリでは、ユーザーがカメラとフォトライブラリから分離されたストレージに写真を保存できるようにしています。次に、各ファイルの名前を取得し、写真を読んでリストに追加します。リストが作成されたら、リストボックスにバインドします。
約5個表示しても問題ありません。スクロールすると、例外が発生します。
System.Windows.Markup.XamlParseException occurred
Message= [Line: 0 Position: 0]
--- Inner Exception ---
KeyNotFoundException
これは私のXAMLです:
<ListBox x:Name="userPhotosListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<ContentControl Content="{Binding Image}" Width="400" />
<Image Name="{Binding FileName}" Source="/Images/appbar.delete.rest.png" Width="48" Height="48"
MouseLeftButtonUp="Image_MouseLeftButtonUp" VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="48" MaxHeight="48" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
これはコードです:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
var userFiles = store.GetFileNames();
foreach (var userFile in userFiles)
{
if (userFile.Contains(PhotoInIsolatedStoragePrefix))
{
var currentBitmap = ReadBitmapImageFromIso(userFile);
var userPhotoImage = new Image { Source = currentBitmap };
var userImg = new Img(userPhotoImage, userFile);
userPhotosListBox.Items.Add(userImg);
}
}
}
public class Img
{
public Img(Image img, string fileName)
{
this.Image = img;
this.FileName = fileName;
}
public Image Image { get; set; }
public string FileName { get; set; }
}
WP7開発は非常に新しく、コードが部分的に機能する理由について混乱しています。