c# - 绑定(bind)到列表框时出现 XamlParseException

标签 c# xaml windows-phone-7 xamlparseexception

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

我可以毫无问题地显示大约 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}"
名称必须以字母或下划线字符 (_) 开头,并且只能包含字母、数字或下划线:look here
我认为您的某些文件名不符合这些原则
请改用其他属性,例如 Tag。

关于c# - 绑定(bind)到列表框时出现 XamlParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6345028/

相关文章:

xaml - Xamarin 窗体外壳 : Flyout v/s TabBar

c# - window.external.notify 传递字符串以外的数据

c# - 为什么我的刚体不会在碰撞时停止移动?

c# - Azure 移动服务 - 自定义 API 在本地运行,但部署到 Azure 时收到 404

c# - Windows Phone 应用程序中用户控件的绑定(bind)属性

wcf - 使用 RESTSharp 上传图像(addFile)

windows-phone-7 - 键盘处于事件状态时 ScrollViewer 不会向上滚动

c# - 在 Xamarin.Mac 中将图像复制到剪贴板

c# - 如果没有 HttpContext 或 ActionContext,如何在 .net core 中构建 URL

c# - WPF应用程序无法识别Window.Resource