ListBoxにバインディングするときのXamlParseException
-
27-10-2019 - |
質問
私のアプリでは、ユーザーがカメラや写真ライブラリから分離されたストレージに写真を保存できるようにしています。次に、各ファイルの名前を取得し、写真を読んでリストに追加します。リストが構築されたら、リストボックスにバインドします。
問題なく約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開発に非常に新しいものであり、私のコードが部分的に機能する理由について混乱しています。
解決
あなたは間違いを犯したと思います: Name="{Binding FileName}"
名前は文字またはアンダースコア文字(_)から始める必要があり、文字、数字、またはアンダースコアのみを含む必要があります。 ここを見て
あなたのファイル名のいくつかはこれらの原則とともに数学ではないと思います
代わりにタグのような別のプロパティを使用します。
他のヒント
この投稿をご覧ください: xamlparseexception私を夢中にさせます!
一番下の行は、多くの場合、XmlParseExceptionが実際にneRexceptionで決定できるターゲットインボケーションであるということです。それはさらなる調査のためのアンカーかもしれません。
使う:
try
{
}
catch(Exception ex)
{
}
キャッチにブレークポイントを構築して設定します。次に、EX変数をより詳細に検査して、より多くの洞察を与える可能性のあるInnerexceptionが含まれているかどうかを確認します。
所属していません StackOverflow