简体   繁体   English

带有MVVM Light的WP7项目中引发的UnauthorizedAccessException(无效的跨线程访问)

[英]UnauthorizedAccessException (Invalid cross-thread access) thrown in WP7 project with MVVM Light

I am using MVVM light toolkit's Messenger class to communicate the different parts of my WP7 application. 我正在使用MVVM light工具箱的Messenger类来传达WP7应用程序的不同部分。

I am trying to make a view go to a different state when receiving a message from a class of my app. 从我的应用程序类接收消息时,我试图使视图进入不同的状态。 In the view's code-behind I register to the message: 在视图的代码背后,我注册了消息:

Messenger.Default.Register<PageActionMessage>
(
     this,
    (action) => GoToPage(action)
);

and call the following method when it is received: 并在收到时调用以下方法:

private object GoToPage(PageActionMessage action)
{
    if (action.action == PageAction.TwitterAuthFinished)
    {
        if (IsoStoreHelper.CheckIfAuthorized())
        {
            VisualStateManager.GoToState(this, "TwitterSendPage", true); // The exception is thrown here
        }
     }
}

In my other class, the message is broadcasted this way: 在我的另一堂课中,消息是通过以下方式广播的:

var PageMsg = new PageActionMessage()
{
    action = PageAction.TwitterAuthFinished
};
Messenger.Default.Send<PageActionMessage>(PageMsg);

This message is correctly received by the view, but when calling the VisualStateManager's GoToState method a UnauthorizedAccessException (Invalid cross-thread access) is thrown with the following trace: 视图正确接收了此消息,但是在调用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()

What is this due to? 这是什么原因 Thanks in advance! 提前致谢!

this is my solution: 这是我的解决方案:

http://wp8xp.blogspot.com.es/2013/02/llamadas-asincronas-internet-haciendo.html http://wp8xp.blogspot.com.es/2013/02/llamadas-asincronas-internet-haciendo.html

Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            });

With MVVVMLight you can use The DispatcherHelper to ensure code runs on the UI thread (your problem). 使用MVVVMLight,您可以使用DispatcherHelper来确保代码在UI线程上运行(您的问题)。
eg 例如

DispatcherHelper.CheckBeginInvokeOnUI(YOUR-ACTION);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM