GetCurrentView()メソッドを使用してディスパッチャーを取得する際に問題が発生します。私のアプリケーションが割り当てられたアクセスモードで実行されている場合、私はCoreApplication.GetCurrentView()を使用する必要がありますMSDNで述べた。タイマーコールバックで使用される場合ディスパッチャーをタイマーコールバックのLockscreen(割り当て済みアクセス)モードにする
しかしGetCurrentView()が例外を引き起こします。
誰もが私はこれが割り当てられているアクセスモードで動作するように得ることができる方法の提案がありますか? ここに私のコードは何かについてのコメントがあり、うまくいきません。
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;
}
// This works fine
OnTimerAsync();
// From 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)
{
// This causes the app to crash in assigned access mode
await CoreApplication.GetCurrentView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => { /* void */ });
}
else
{
// This works fine in desktop mode, but doesn't work in assigned access mode (lockscreen app)
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => { /* void */ });
// This line below causes app to crash when called from timer callback, but works fine when called directly from OnLaunched();
var curView = CoreApplication.GetCurrentView();
/*
* Exception details when called from 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。 これはタイマーの例ですが、NFC/SmartCardや他の非同期ライブラリも使用していますが、イベントトリガーで同様の問題があります。