当手机离开或进入地理位置时,我正在尝试showToast (这是设置在其他地方并传入的)。问题是,当应用程序在后台时,触发器不会发生,我也看不到showToast消息。我正在用我的电脑上的仿真器手动改变位置。
背景Tasks>位置设置在应用程序清单下面。
这是我用来构建Geofence和背景任务的代码。
//Creates Geofence and names it "PetsnikkerVacationFence"
public static async Task SetupGeofence(double lat, double lon)
{
await RegisterBackgroundTasks();
if (IsTaskRegistered())
{
BasicGeoposition position = new BasicGeoposition();
position.Latitude = lat;
position.Longitude = lon;
double radius = 8046.72; //5 miles in meters
Geocircle geocircle = new Geocircle(position, radius);
MonitoredGeofenceStates monitoredStates = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;
Geofence geofence = new Geofence("PetsnikkerVacationFence", geocircle, monitoredStates, false);
GeofenceMonitor monitor = GeofenceMonitor.Current;
var existingFence = monitor.Geofences.SingleOrDefault(f => f.Id == "PetsnikkerVacationFence");
if (existingFence != null)
monitor.Geofences.Remove(existingFence);
monitor.Geofences.Add(geofence);
}
}
//Registers the background task with a LocationTrigger
static async Task RegisterBackgroundTasks()
{
var access = await BackgroundExecutionManager.RequestAccessAsync();
if (access == BackgroundAccessStatus.Denied)
{
}
else
{
var taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "PetsnikkerVacationFence";
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));
taskBuilder.TaskEntryPoint = typeof(Petsnikker.Windows.Background.GeofenceTask).FullName;
var registration = taskBuilder.Register();
registration.Completed += (sender, e) =>
{
try
{
e.CheckResult();
}
catch (Exception error)
{
Debug.WriteLine(error);
}
};
}
}
static bool IsTaskRegistered()
{
var Registered = false;
var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(keyval => keyval.Value.Name == "PetsnikkerVacationFence");
if (entry.Value != null)
Registered = true;
return Registered;
}
}
}这段代码是我监视地理位置状态的地方。这就是appx清单中的入口点所指向的位置。
public sealed class GeofenceTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var monitor = GeofenceMonitor.Current;
if (monitor.Geofences.Any())
{
var reports = monitor.ReadReports();
foreach (var report in reports)
{
switch (report.NewState)
{
case GeofenceState.Entered:
{
ShowToast("Approaching Home",":-)");
break;
}
case GeofenceState.Exited:
{
ShowToast("Leaving Home", ":-)");
break;
}
}
}
}
//deferral.Complete();
}
private static void ShowToast(string firstLine, string secondLine)
{
var toastXmlContent =
ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var txtNodes = toastXmlContent.GetElementsByTagName("text");
txtNodes[0].AppendChild(toastXmlContent.CreateTextNode(firstLine));
txtNodes[1].AppendChild(toastXmlContent.CreateTextNode(secondLine));
var toast = new ToastNotification(toastXmlContent);
var toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.Show(toast);
Debug.WriteLine("Toast: {0} {1}", firstLine, secondLine);
}
}发布于 2016-04-01 10:32:05
在查看您的代码之后,您的代码似乎是正确的。
为了启动Geofence背景任务以显示吐司信息,请确保以下内容:
1) --请确保您已经在Package.appxmanifest中完成了注册BackgroundTask的所有必要配置,例如,您设置了正确的EntryPoint并添加了“Location”功能。
有关详细信息,您可以尝试将您的Package.appxmanifest与官方示例地理定位的Package.appxmanifest进行比较。也请检查:创建和注册后台任务和在应用程序清单中声明后台任务。
2)请确保您知道如何手动设置模拟器中的位置,以模拟手机离开或输入地理位置。有关如何在模拟器中设置位置的更多信息,请查看以下文章:驾驶。
3) --请确保您在模拟器中的第二个位置与您第一次定义的地理位置相差不远,因为模拟器的行为就像一个真正的设备,并且该设备不会突然从纽约移到西雅图。否则BackgroundTask就不会马上被火烧了。
4)用于地理定位的后台任务不能比每2分钟更频繁地启动。如果在后台测试地理信息,则模拟器能够自动启动后台任务。但是对于下一个后台任务,您需要等待超过2分钟。
此外,我将推荐您参考以下文章,介绍如何使用Windows仿真程序测试具有地理位置的应用程序:https://blogs.windows.com/buildingapps/2014/05/28/using-the-windows-phone-emulator-for-testing-apps-with-geofencing/。
谢谢。
https://stackoverflow.com/questions/36161960
复制相似问题