2017-08-14 11 views
0

GetCurrentView()メソッドを使用してディスパッチャーを取得する際に問題が発生します。私のアプリケーションが割り当てられたアクセスモードで実行されている場合、私はCoreApplication.GetCurrentView()を使用する必要がありますMSDNで述べた。タイマーコールバックで使用される場合ディスパッチャーをタイマーコールバックのLockscreen(割り当て済みアクセス)モードにする

https://msdn.microsoft.com/windows/hardware/drivers/partnerapps/create-a-kiosk-app-for-assigned-access

しかし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や他の非同期ライブラリも使用していますが、イベントトリガーで同様の問題があります。

答えて

0

MvvmLightのDispatcherHelperクラスを使用してGetCurrentView()メソッドを置き換えて問題を解決しました。

このクラスは、その後、あなたが(私はGetCurrentView()が行うことになった-thought-例えば正確に何)それを必要な場所から使用されているアプリの起動時にディスパッチャへの静的参照を設定します

は(これを参照してください。優れた)記事:

http://msdn.microsoft.com/en-us/magazine/dn630646.aspx?f=255&MSPPError=-2147217396

関連する問題