简体   繁体   中英

wp7 cannot get current page from background worker

Unfortunately, in windows phone 7 there's no "Broadcast Receiver" concept as in android, so I'm trying to do something like it.

In my application, I run a background worker that calls a function and upon finishing I have to make a refresh some data in a page. I don't know if the required page will be the current page at finish time so I have to make a test. I found this code in some forum:

var frame = Application.Current.RootVisual as PhoneApplicationFrame;
var startPage = frame.Content as PhoneApplicationPage;

this gives me a ' System.UnauthorizedAccessException ' exception on runtime. In fact, accessing all Application.Current properties gives the same exception. What I'm trying to understand, is that if I can't access the current page from a background worker or thread why would I need it anyway ? If I'm in main UI thread I know in what page I stand right ? Am I missing something here?

Please help

You can only access Application.Current from the UI thread. Use Dispatcher.BeginInvoke from your background thread to delegate the execution to the UI thread:

Deployment.Current.Dispatcher.BeginInvoke(() => 
{
    // Your code needing Application.Current
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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