2016-05-05 4 views
0

ジオフェンスの変更を処理するためにgeofence APIとバックグラウンドタスクを使用して簡単なアプリケーションを構築します。ここでは、コードの一部である、ウィッヒバックグラウンドタスク新しいLocationTrigger(LocationTriggerType.Geofence)は、バックグラウンドタスクを登録するときにInvalidCastExceptionで失敗する

private async void RegisterBackgroundTask() 
{ 
    const string name = "GeofenceBackgroundTask"; 

    if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == name)) 
    { 
     return; 
    } 

    var loc = await new Geolocator().GetGeopositionAsync(
     TimeSpan.FromMinutes(2), 
     TimeSpan.FromSeconds(5)); //needed to trig user acceptance 

    var backgroundAccessStatus = 
     await BackgroundExecutionManager.RequestAccessAsync(); 

    if (backgroundAccessStatus != BackgroundAccessStatus.Denied) 
    { 
     var geofenceTaskBuilder = new BackgroundTaskBuilder() 
     { 
      Name = name, 
      TaskEntryPoint = "RingtoneManager.Background.GeofenceBackgroundTask" 
     }; 

     geofenceTaskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence)); 
     geofenceTaskBuilder.Register(); 
    } 
} 

を登録しようとすると、この行は、このような、私はトリガーの上に登録してみますInvalidCastExceptionが

System.InvalidCastException was unhandled by user code 
HResult=-2147467262 
Message=Unable to cast object of type 'System.__ComObject' to type 'Windows.ApplicationModel.Background.ILocationTriggerFactory'. 
Source=mscorlib 
StackTrace: 
    at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget) 
    at Windows.ApplicationModel.Background.LocationTrigger..ctor(LocationTriggerType triggerType) 
    at RingtoneManager3.App.<RegisterBackgroundTask>d__2.MoveNext() 
InnerException: 

new LocationTrigger(LocationTriggerType.Geofence) 

に失敗常にありますシステム時間トリガーと例外はありませんでした。

私は間違っていますか?

答えて

0

まず、ジオクルクルを作成する必要があると思います。

public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default") 
     { 
      // The Geofence is a circular area centered at (latitude, longitude) point, with the 
      // radius in meter. 
      var geocircle = new Geocircle(position, radius); 

     // Sets the events that we want to handle: in this case, the entrace and the exit 
     // from an area of intereset. 
     var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited; 

     // Specifies for how much time the user must have entered/exited the area before 
     // receiving the notification. 
     var dwellTime = TimeSpan.FromSeconds(1); 

     // Creates the Geofence and adds it to the GeofenceMonitor. 
     var geofence = new Geofence(id, geocircle, mask, false, dwellTime); 

     try 
     { 
      GeofenceMonitor.Current.Geofences.Add(geofence); 
     } 
     catch (Exception e) 
     { 
      //Debug.WriteLine(e); 
      // geofence already added to system 
     } 
    } 

し、各場所のためにご参加

CreateGeofence(new BasicGeoposition() { Longitude = double.Parse(VersionObject.store.lng), Latitude = double.Parse(VersionObject.store.lat) }, 200); 
          try 
          { 
           var backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync(); 
           var geofenceBuilder = new BackgroundTaskBuilder 
           { 
            Name = "Test Geofence", 
            TaskEntryPoint = "GeofencingTask.BackgroundGeofencing" 
           }; 
           var trigger = new LocationTrigger(LocationTriggerType.Geofence); 
           geofenceBuilder.SetTrigger(trigger); 
           var geofenceTask = geofenceBuilder.Register(); 
          } 
          catch (Exception ex) 
          { 
           System.Diagnostics.Debug.WriteLine(ex.Message); 
          } 
+0

おかげで、ジオフェンスを作成します。私はあなたの提案を試み、それは問題を解決しません。また、最初の投稿にエラーの説明を追加します – mshipov

+0

場所に適切な権限を与えましたか? – Rohit

+0

はい、場所のアクセス許可は必須とマークされています – mshipov

関連する問題