Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XamlParseException when binding to listbox

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.

like image 293
Cloud SME Avatar asked Nov 13 '22 21:11

Cloud SME


1 Answers

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.

like image 85
Navid Rahmani Avatar answered Dec 19 '22 02:12

Navid Rahmani