我在数据绑定期间遇到一个奇怪的未处理异常.我将ListBox与IList数据绑定.在使用对象填充列表后,我将ListBox的DataContext设置为我的对象:
IList<Users> users = new List<Users>;
foreach(JToken jresult in Users)
{
User juser = JsonConvert.DeserializeObject<User>(jresult.ToString());
users.Add(juser);
this.DataContext = myObject;
Run Code Online (Sandbox Code Playgroud)
我的对象只有两个字段,名称和电子邮件:
public class User
{
public string name { get; set; }
public string email { get; set; } }
Run Code Online (Sandbox Code Playgroud)
在XAML方面,我绑定ListItem和ItemTemplate中的文本块,如下所示:
<ListBox Name="Users" ItemsSource="{Binding}">
<TextBlock Text="{Binding Name}" Name="name" />
<TextBlock Text="{Binding Email}" Name="email" />
Run Code Online (Sandbox Code Playgroud)
在调试过程中,我可以看到用户列表框中填充了正确的项目,但是在退出方法后,应用程序崩溃了,我直接进入App.xaml.cs方法Application_UnhandledException.错误详细信息如下:
e.ExceptionObject.InnerException
{"0x8000ffff"}
_data: null
_HResult: -2146233088
_innerException: null
_message: "0x8000ffff"
_methodDescs: {System.IntPtr[14]}
_optionalData: null
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
InnerException: Could not evaluate expression
Message: "0x8000ffff"
StackTrace: …Run Code Online (Sandbox Code Playgroud)