【发布时间】:2014-07-03 23:02:45
【问题描述】:
我有一个非常简单的应用程序,但我在使用数据绑定时遇到了问题。我已成功下载使用此示例:http://msdn.microsoft.com/en-us/magazine/hh852595.aspx
有“next”按钮,生成新人并将其加载到应用程序中。
但是我尝试做同样的事情,但出现以下异常:System.UnauthorizedAccessException - Invalid cross-thread access
我也尝试过这样做:
public Chat()
{
InitializeComponent();
bindingChat.leftText = "jooooo2";
ContentPanel.DataContext = bindingChat;
}
private void connect(object sender, XmppConnectedEventArgs target)
{
bindingChat = new BindingChat();
bindingChat.leftText = "Connected";
ContentPanel.DataContext = bindingChat; //this is where the exception is thrown
}
文本“jooooo2”按预期工作,但调用connet方法时,出现上述异常。
在该示例中,他们使用以下代码设置了新人(单击按钮后):
private void SetDataContext()
{
GeneratePerson();
ContentPanel.DataContext = _currentPerson;
}
而且效果很好。
编辑:
好的,我发现是因为它是用这个间接调用的:
private void Button_Click(object sender, RoutedEventArgs e)
{
xmpp1.OnConnected += new Xmpp.OnConnectedHandler(connect);
xmpp1.IMServer = "***";
xmpp1.IMPort = 5222;
xmpp1.Connect("user1", "heslouser1");
xmpp1.ChangePresence(1, "I'm here!");
}
如果我尝试使用另一个按钮直接更改它,它会按预期工作:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
bindingChat = new BindingChat();
bindingChat.leftText = "button pressed";
ContentPanel.DataContext = bindingChat;
}
【问题讨论】:
标签: c# visual-studio windows-phone-8