UnauthorizedAccessException (acceso de subprocesos cruzados no válido) lanzado en el proyecto WP7 con MVVM Light

I am using MVVM light toolkit's Messenger class to communicate the different parts of my WP7 application.

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:

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!

preguntado el 02 de febrero de 12 a las 10:02

Your code is trying to access and set the VisualState on the UI thread from a different Thread, try to use a Dispatcher on the VisualStateManager call. -

2 Respuestas

With MVVVMLight you can use The DispatcherHelper to ensure code runs on the UI thread (your problem).
p.ej

DispatcherHelper.CheckBeginInvokeOnUI(YOUR-ACTION);

Respondido 02 Feb 12, 15:02

esta es mi solución:

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

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

Respondido 02 Feb 13, 21:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.