C# 在带有MVVM指示灯的WP7项目中引发UnauthorizedAccessException(无效的跨线程访问)

C# 在带有MVVM指示灯的WP7项目中引发UnauthorizedAccessException(无效的跨线程访问),c#,mvvm,mvvm-light,windows-phone-7.1,C#,Mvvm,Mvvm Light,Windows Phone 7.1,我正在使用MVVM light toolkit的Messenger类与WP7应用程序的不同部分进行通信 当我从我的应用程序的某个类收到消息时,我试图使视图进入不同的状态。在视图的代码隐藏中,我注册到消息: Messenger.Default.Register<PageActionMessage> ( this, (action) => GoToPage(action) ); 在我的另一节课上,信息是这样广播的: var PageMsg = new PageA

我正在使用MVVM light toolkit的Messenger类与WP7应用程序的不同部分进行通信

当我从我的应用程序的某个类收到消息时,我试图使视图进入不同的状态。在视图的代码隐藏中,我注册到消息:

Messenger.Default.Register<PageActionMessage>
(
     this,
    (action) => GoToPage(action)
);
在我的另一节课上,信息是这样广播的:

var PageMsg = new PageActionMessage()
{
    action = PageAction.TwitterAuthFinished
};
Messenger.Default.Send<PageActionMessage>(PageMsg);
var PageMsg=new PageActionMessage()
{
action=PageAction.TwitterAuthFinished
};
Messenger.Default.Send(PageMsg);
视图正确地接收到此消息,但调用VisualStateManager的GoToState方法时,会引发UnauthorizedAccessException(无效的跨线程访问),并带有以下跟踪:

System.UnauthorizedAccessException was unhandled
  Message=Invalid cross-thread access.
 StackTrace:
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
   at System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at System.Windows.Controls.Panel.get_Children()
   at MS.Internal.XcpImports.CheckThread()
   at MS.Internal.XcpImports.Control_GetImplementationRoot(Control control)
   at System.Windows.Controls.Control.get_ImplementationRoot()
   at System.Windows.VisualStateManager.GoToState(Control control, String stateName, Boolean useTransitions)
   at KidsBook.MainPage.GoToPage(PageActionMessage action)
   at KidsBook.MainPage.<.ctor>b__0(PageActionMessage action)
   at GalaSoft.MvvmLight.Helpers.WeakAction`1.Execute(PageActionMessage parameter)
   at GalaSoft.MvvmLight.Helpers.WeakAction`1.ExecuteWithObject(Object parameter)
   at GalaSoft.MvvmLight.Messaging.Messenger.SendToList[TMessage](PageActionMessage message, IEnumerable`1 list, Type messageTargetType, Object token)
   at GalaSoft.MvvmLight.Messaging.Messenger.SendToTargetOrType[TMessage](PageActionMessage message, Type messageTargetType, Object token)
   at GalaSoft.MvvmLight.Messaging.Messenger.Send[TMessage](PageActionMessage message)
   at KidsBook.Twitter.OAuthClient.GetResponse(IAsyncResult result)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
未处理System.UnauthorizedAccess异常 消息=无效的跨线程访问。 堆栈跟踪: 在MS.Internal.XcpImports.CheckThread()处 位于System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp) 位于System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp) 位于System.Windows.DependencyObject.GetValue(DependencyProperty dp) 在System.Windows.Controls.Panel.get_Children()中 在MS.Internal.XcpImports.CheckThread()处 在MS.Internal.XcpImports.Control\u GetImplementationRoot(控制) 在System.Windows.Controls.Control.get_ImplementationRoot()中 位于System.Windows.VisualStateManager.GoToState(控件控件、字符串状态名、布尔值) 在KidsBook.MainPage.GoToPage(页面操作消息操作) 在KidsBook.MainPage.b___0(页面操作消息操作) 在GalaSoft.MvvmLight.Helpers.WeakAction`1.Execute处(PageActionMessage参数) 在GalaSoft.MvvmLight.Helpers.WeakAction`1.ExecuteWithObject(对象参数) 在GalaSoft.MvvmLight.Messaging.Messenger.SendToList[TMessage](PageActionMessage消息,IEnumerable`1列表,键入messageTargetType,对象令牌) 在GalaSoft.MvvmLight.Messaging.Messenger.SendtoTargetType[TMessage](PageActionMessage消息,键入messageTargetType,对象令牌) 在GalaSoft.MvvmLight.Messaging.Messenger.Send[TMessage](PageActionMessage消息)上 在KidsBook.Twitter.OAuthClient.GetResponse(IAsyncResult结果) 在System.Net.Browser.ClientHttpWebRequest.c__中显示ClassA.b__8(对象状态2) 位于System.Threading.ThreadPool.WorkItem.WaitCallback\u上下文(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadPool.WorkItem.doWork(对象o)处 在System.Threading.Timer.ring()中
这是因为什么?提前谢谢

使用MVVVMLight,您可以使用
DispatcherHelper
确保代码在UI线程上运行(您的问题)。
e、 g


使用MVVVMLight,您可以使用
DispatcherHelper
确保代码在UI线程上运行(您的问题)。
e、 g

这是我的解决方案:

这是我的解决方案:


您的代码尝试从其他线程访问和设置UI线程上的VisualState,请尝试在VisualStateManager调用上使用调度程序。您的代码尝试从其他线程访问和设置UI线程上的VisualState,请尝试在VisualStateManager调用上使用调度程序。
System.UnauthorizedAccessException was unhandled
  Message=Invalid cross-thread access.
 StackTrace:
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
   at System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at System.Windows.Controls.Panel.get_Children()
   at MS.Internal.XcpImports.CheckThread()
   at MS.Internal.XcpImports.Control_GetImplementationRoot(Control control)
   at System.Windows.Controls.Control.get_ImplementationRoot()
   at System.Windows.VisualStateManager.GoToState(Control control, String stateName, Boolean useTransitions)
   at KidsBook.MainPage.GoToPage(PageActionMessage action)
   at KidsBook.MainPage.<.ctor>b__0(PageActionMessage action)
   at GalaSoft.MvvmLight.Helpers.WeakAction`1.Execute(PageActionMessage parameter)
   at GalaSoft.MvvmLight.Helpers.WeakAction`1.ExecuteWithObject(Object parameter)
   at GalaSoft.MvvmLight.Messaging.Messenger.SendToList[TMessage](PageActionMessage message, IEnumerable`1 list, Type messageTargetType, Object token)
   at GalaSoft.MvvmLight.Messaging.Messenger.SendToTargetOrType[TMessage](PageActionMessage message, Type messageTargetType, Object token)
   at GalaSoft.MvvmLight.Messaging.Messenger.Send[TMessage](PageActionMessage message)
   at KidsBook.Twitter.OAuthClient.GetResponse(IAsyncResult result)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
DispatcherHelper.CheckBeginInvokeOnUI(YOUR-ACTION);
Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            });