Windows phone 8.1 如何在windows phone silverlight 8.1上添加地理围栏

Windows phone 8.1 如何在windows phone silverlight 8.1上添加地理围栏,windows-phone-8.1,geofencing,Windows Phone 8.1,Geofencing,我想在我的Windows Phone 8项目上实现地理围栏,我知道针对Windows Phone 8.1启动了新的地理围栏API,所以我将我的项目更新为Windows Phone 8.1。现在我的项目变成了Windows phone Silverlight 8.1。我想在后台触发通知,并在用户到达他们想要的位置时放弃通知。我在谷歌上搜索了很多,但我在WindowsPhone8.1上找到的都不是silverlight 8.1。我尝试将这些示例添加到我的应用程序中,这样它就不会出现任何错误,我的应用

我想在我的Windows Phone 8项目上实现地理围栏,我知道针对Windows Phone 8.1启动了新的地理围栏API,所以我将我的项目更新为Windows Phone 8.1。现在我的项目变成了Windows phone Silverlight 8.1。我想在后台触发通知,并在用户到达他们想要的位置时放弃通知。我在谷歌上搜索了很多,但我在WindowsPhone8.1上找到的都不是silverlight 8.1。我尝试将这些示例添加到我的应用程序中,这样它就不会出现任何错误,我的应用程序工作正常,但不会触发geofence。。。有人能帮我吗

在这里,我在我的应用程序中尝试的可能是,你也可以帮助我

首先,我用下面的代码创建了一个类

public class GeofenceService
{
    public static IList<Geofence> GetGeofence()
    {
        return GeofenceMonitor.Current.Geofences;
    }

    public static void CreateGeofence(string id, double lat, double lon, double radius)
    {
        if (GeofenceMonitor.Current.Geofences.FirstOrDefault(i => i.Id == id) != null) return;

        var position = new BasicGeoposition();
        position.Latitude = lat;
        position.Longitude = lon;

        var geocircle = new Geocircle(position, radius);

        MonitoredGeofenceStates mask = 0;
        mask |= MonitoredGeofenceStates.Entered;

        var geofence = new Geofence(id, geocircle, mask, false, new TimeSpan(0, 0, 5));
        GeofenceMonitor.Current.Geofences.Add(geofence);
    }

    public static void RemoveGeofence(string id)
    {
        var geofence = GeofenceMonitor.Current.Geofences.SingleOrDefault(g => g.Id == id);

        if (geofence != null)
            GeofenceMonitor.Current.Geofences.Remove(geofence);
    }

}
最后,我将调用Geofence函数来添加地理定位和注册后台任务,并且我还将在Packege.appxmanifest上正确声明。但仍然不起作用:( 谢谢你的帮助

public sealed class LocationTask : IBackgroundTask
{
    static string TaskName = "GeofenceUniversalAppLocationTask";

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // Uncomment this to utilize async/await in the task
        //var deferral = taskInstance.GetDeferral();

        // Get the information of the geofence(s) that have been hit
        var reports = GeofenceMonitor.Current.ReadReports();
        var report = reports.FirstOrDefault(r => (r.Geofence.Id == "MicrosoftStudioE") && (r.NewState == GeofenceState.Entered));

        if (report == null) return;

        // Create a toast notification to show a geofence has been hit
        var toastXmlContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

        var txtNodes = toastXmlContent.GetElementsByTagName("text");
        txtNodes[0].AppendChild(toastXmlContent.CreateTextNode("Geofence triggered toast!"));
        txtNodes[1].AppendChild(toastXmlContent.CreateTextNode(report.Geofence.Id));

        var toast = new ToastNotification(toastXmlContent);
        var toastNotifier = ToastNotificationManager.CreateToastNotifier();
        toastNotifier.Show(toast);

        // Uncomment this to utilize async/await in the task
        //deferral.Complete();
    }

    public async static void Register()
    {
        if (!IsTaskRegistered())
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();
            var builder = new BackgroundTaskBuilder();

            builder.Name = TaskName;
            builder.TaskEntryPoint = typeof(LocationTask).FullName;
            builder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));

            // Uncomment this if your task requires an internet connection
            //var condition = new SystemCondition(SystemConditionType.InternetAvailable);
            //builder.AddCondition(condition);

            builder.Register();
        }
    }

    public static void Unregister()
    {
        var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(kvp => kvp.Value.Name == TaskName);

        if (entry.Value != null)
            entry.Value.Unregister(true);
    }

    public static bool IsTaskRegistered()
    {
        var taskRegistered = false;
        var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(kvp => kvp.Value.Name == TaskName);

        if (entry.Value != null)
            taskRegistered = true;

        return taskRegistered;
    }
}