5

在我的应用程序中,我允许用户将照片从相机和照片库保存到独立存储中。然后我获取每个文件的名称并阅读照片并添加到我的列表中。构建列表后,我将其绑定到列表框。

我可以毫无问题地显示大约 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 开发非常陌生,并且对我的代码部分有效的原因感到困惑。

4

2 回答 2

1

我认为您犯了一个错误:Name="{Binding FileName}"
名称必须以字母或下划线字符 (_) 开头,并且只能包含字母、数字或下划线:看这里
我认为您的某些文件名不符合这些原则
使用另一个像 Tag 这样的属性。

于 2011-06-17T03:04:51.587 回答
0

查看这篇文章:XAMLParseException 让我发疯!

底线是 XmlParseException 通常实际上是一个 TargetInvocationException,可以在 InnerException 中确定。这可能是进一步调查的依据。

用一个:

try
{
}
catch(Exception ex)
{
}

在 catch 处构造并设置断点。然后更详细地检查 ex 变量,看看它是否包含一个可能会给你更多洞察力的 InnerException。

于 2011-06-16T21:02:00.997 回答