c# - Getting Dispatcher in Lockscreen (Assigned-access) mode in a timer callback -
i'm having trouble getting dispatcher using getcurrentview() method. stated on msdn should using coreapplication.getcurrentview() if app running in assigned access mode.
however getcurrentview() causes exception when used in timer callback.
anybody have suggestion how can work in assigned access mode? here's code comments does, , doesn't work.
sealed partial class app : application { private timer timer; public app() { this.initializecomponent(); } protected override void onlaunched(launchactivatedeventargs e) { try { isassignedaccess = lockapplicationhost.getforcurrentview() != null; } catch { isassignedaccess = false; } // works fine ontimerasync(); // timer callback ontimerasync() doesn't work (see below) this.timer = new timer((e2) => { ontimerasync(); }, null, (int)timespan.fromseconds(5).totalmilliseconds, (int)timespan.fromseconds(5).totalmilliseconds); } private async void ontimerasync() { if (app.isassignedaccess) { // causes app crash in assigned access mode await coreapplication.getcurrentview().dispatcher.runasync(coredispatcherpriority.normal, () => { /* void */ }); } else { // works fine in desktop mode, doesn't work in assigned access mode (lockscreen app) await coreapplication.mainview.corewindow.dispatcher.runasync(coredispatcherpriority.normal, () => { /* void */ }); // line below causes app crash when called timer callback, works fine when called directly onlaunched(); var curview = coreapplication.getcurrentview(); /* * exception details when called timer callback * exception thrown: 'system.runtime.interopservices.comexception' in lockscreentest.exe * winrt information: kan element niet vinden. (translated: cannot find element) */ var curdispatch = curview.dispatcher; await curdispatch.runasync(coredispatcherpriority.normal, () => { /* void */ }); } } public static bool isassignedaccess { get; private set; } }
ps. example timer, i'm using nfc/smartcard , other async libraries have similar problems on event triggers.
i solved issue using dispatcherhelper class mvvmlight replace getcurrentview() method.
this class sets static reference dispatcher upon app startup later used wherever need (e.g. -thought- getcurrentview() supposed do)
see (excellent) article more details:
http://msdn.microsoft.com/en-us/magazine/dn630646.aspx?f=255&mspperror=-2147217396
Comments
Post a Comment