如何解决绑定到列表框时XamlParseException
| 在我的应用程序中,我允许用户将照片从相机和照片库保存到隔离的存储中。然后,我得到每个文件的名称,并阅读照片并添加到我的列表中。建立列表后,我将其绑定到列表框。 我可以得到大约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}\"
名称必须以字母或下划线字符(_)开头,并且只能包含字母,数字或下划线:请看此处
我认为您的某些文件名不符合这些原则
请改用其他属性,例如Tag。
, 看看这篇文章:XAMLParseException驱使我疯狂!
最重要的是,通常XmlParseException实际上是可以在InnerException中确定的TargetInvocationException。这可能是进一步调查的基础。
用一个:
try
{
}
catch(Exception ex)
{
}
在捕获处构造并设置一个断点。然后更详细地检查ex变量,以查看它是否包含InnerException,它可以为您提供更多信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。