XamlParseException when binding to listbox
-
27-10-2019 - |
Pergunta
In my app I am allowing users to save photos from camera and photo library to isolated storage. I then get the name of each file and read the photo and add to my list. Once the list is built, I bind it to the list box.
I can get about 5 displayed without a problem. After I scroll I get the exception:
System.Windows.Markup.XamlParseException occurred
Message= [Line: 0 Position: 0]
--- Inner Exception ---
KeyNotFoundException
This is my 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>
This is the code:
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; }
}
Very new to WP7 development and confused as to why my code partially works.
Solução
I think you made a mistake in: Name="{Binding FileName}"
Name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores: look here
I think some of your file names are not math with these principles
Use another property like Tag instead.
Outras dicas
Check out this post: XAMLParseException driving me CRAZY!
The bottom line is that often a XmlParseException is actually a TargetInvocationException which can be determined in the InnerException. That could be an anchor for further investigation.
Use a:
try
{
}
catch(Exception ex)
{
}
construct and set a breakpoint at the catch. Then inspect the ex variable in greater detail to see if it contains a InnerException that may give you more insight.