如何解决Xamarin表单中无法使用闪亮的地理围栏
我实际上是在使用https://allancritchie.net/posts/shiny-geofencing为我的Xamarin表单应用程序实现地理围栏,但是在“ GeofenceDelegate.cs”中并没有遇到断点。
谁能告诉我我的代码出了什么问题? 我正在android模拟器中进行测试。
using MyApp.Services.BackGroundJobs;
using Microsoft.Extensions.DependencyInjection;
using Shiny;
namespace MyApp
{
public class MyAppStart : ShinyStartup
{
public override void ConfigureServices(IServiceCollection builder)
{
builder.UseGeofencing<GeofenceDelegate>(true);
builder.UseNotifications(true);
}
}
}
using Shiny.Locations;
using Shiny.Notifications;
using System.Threading.Tasks;
namespace MyApp.Services.BackGroundJobs
{
public class GeofenceDelegate : IGeofenceDelegate
{
readonly INotificationManager notifications;
public GeofenceDelegate(INotificationManager notifications)
{
this.notifications = notifications;
}
public async Task OnStatusChanged(GeofenceState newStatus,GeofenceRegion region)
{
await this.notifications.Send(new Notification
{
Title = "Working",Message = "It is good to have you back " + region.Identifier
});
if (newStatus == GeofenceState.Entered)
{
await this.notifications.Send(new Notification
{
Title = "WELCOME!",Message = "It is good to have you back " + region.Identifier
});
}
else
{
await this.notifications.Send(new Notification
{
Title = "GOODBYE!",Message = "You will be missed at " + region.Identifier
});
}
}
}
}
MainActivity.cs
public override void OnRequestPermissionsResult(int requestCode,string[] permissions,Permission[] grantResults)
{
AndroidShinyHost.OnRequestPermissionsResult(requestCode,permissions,grantResults);
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode,grantResults);
}
MainApplication.cs
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
AndroidShinyHost.Init(this,new MyAppStart(),services => {
// register any platform specific stuff you need here
});
//A great place to initialize Xamarin.Insights and Dependency Services!
}
查看模型代码
private async void Checkgeofences()
{
// shiny doesn't usually manage your viewmodels,so we'll do this for now
var geofences = ShinyHost.Resolve<IGeofenceManager>();
var notifications = ShinyHost.Resolve<INotificationManager>();
// this is really only required on iOS,but do it to be safe
var access = await notifications.RequestAccess();
if (access == AccessState.Available)
{
await notifications.Send(new Notification
{
Title = "WELCOME!",Message = "Start GeofenceRegion"
});
await geofences.StartMonitoring(new GeofenceRegion(
"CN Tower - Toronto,Canada",new Position(23.7430,21.2363),Distance.FromMeters(3)
)
{
NotifyOnEntry = true,NotifyOnExit = true,SingleUse = false
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。