我已经阅读了我遇到的这个问题,但我似乎无法找到解决方案。有人建议使用调度程序将代码执行推送到 UI 线程。但是,这并不能解决我的问题。我从一个 Asyc 方法中调用此代码,该方法之前由 Guide 对象调用以显示通知。控制 UI 的代码如下:
public bool isDisabled
{
set
{
if (value)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
color = new SolidColorBrush(Colors.Gray);
});
enabled = false;
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
color = origionalColor;
});
enabled = true;
}
}
}
它是从
private void nameInputFinished(IAsyncResult ar)
{
int? result = Guide.EndShowMessageBox(ar);
if (result == 0)
{
[...]
JourneyPanels.getPanel(JourneyPanelTypes.integration).isDisabled = false;
}
}
不知道为什么我得到了跨线程异常,但我很确定它与异步方法嵌套有关。
任何帮助是极大的赞赏!
编辑:完整的异常堆栈
{System.UnauthorizedAccessException: Invalid cross-thread access.
at MS.Internal.XcpImports.CheckThread()
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, Boolean b)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, Boolean b)
at System.Windows.Controls.TextBox.set_IsReadOnly(Boolean value)
at PocketRitual.Journey.nameInputFinished(IAsyncResult ar)
at Microsoft.Xna.Framework.GamerServices.ActionDialogHelper.Complete()
at Microsoft.Xna.Framework.GamerServices.ActionDialogHelper.GetMessageBoxResult(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}