Xamarin - 地理定位不起作用......但为什么 [英] Xamarin - Geolocation doesnt work... But why
问题描述
我只是想开发一个小应用程序,这给了我目前的职位.所以我找到了这个插件,我输入到我的代码中.现在,我得到了所有要编译的东西 - 应用程序启动了,但没有警告就结束了.没有崩溃,什么都没有.甚至 Xamarin 也没有出现任何崩溃的迹象.你们能帮我吗?我什么都没试过,我完全没有想法......;)再次感谢!
I am just trying to develope a little app, that gives me my current position. So I found this plugin that I entered to my code. Now, that I got everything to compile - the app starts but then, without warning ends. No crash, no nothing. Even Xamarin doesnt show any sign of crash. Can you guys help me out? I have tried nothing, and Im all out of ideas... ;) thanks again!
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//Initialize buttons:
Button StartButton = FindViewById<Button>(Resource.Id.startbutton);
TextView txtTestGps = FindViewById<TextView>(Resource.Id.GpsTest);
ShowGpsCoordinates(StartButton, txtTestGps);
}
private void ShowGpsCoordinates(Button StartButton, TextView txtTestGps)
{
Task<double> xy = GiveGpsLocation();
double xyOut = xy.Result;
StartButton.Click += (sender, e) =>
{
txtTestGps.Text = xyOut.ToString();
};
}
private async Task<double> GiveGpsLocation()
{
double DoubleWithCoordinates = 0.0;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(10000);
// Console.WriteLine("Position Status: {0}", position.Timestamp);
// Console.WriteLine("Position Latitude: {0}", position.Latitude);
// Console.WriteLine("Position Longitude: {0}", position.Longitude);
DoubleWithCoordinates = position.Latitude;
return DoubleWithCoordinates;
}
}
推荐答案
由于您没有等待对异步方法的所有调用,因此可能会吞下异常,从而隐藏崩溃的原因.
Since you are not awaiting all calls to async methods, exceptions might be getting swallowed, hiding the cause of the crash.
也许可以尝试以下操作,看看是否可以得到一个可以告诉您一些信息的异常:
Perhaps try the following and see if you can get an exception that tells you something:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//Initialize buttons:
Button StartButton = FindViewById<Button>(Resource.Id.startbutton);
TextView txtTestGps = FindViewById<TextView>(Resource.Id.GpsTest);
Task.Run(async () => {
await ShowGpsCoordinates(StartButton, txtTestGps);
}
}
private async Task ShowGpsCoordinates(Button StartButton, TextView txtTestGps)
{
double xyOut = await GiveGpsLocation();
StartButton.Click += (sender, e) =>
{
txtTestGps.Text = xyOut.ToString();
};
}
private async Task<double> GiveGpsLocation()
{
double DoubleWithCoordinates = 0.0;
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(10000);
// Console.WriteLine("Position Status: {0}", position.Timestamp);
// Console.WriteLine("Position Latitude: {0}", position.Latitude);
// Console.WriteLine("Position Longitude: {0}", position.Longitude);
DoubleWithCoordinates = position.Latitude;
return DoubleWithCoordinates;
}
}
这篇关于Xamarin - 地理定位不起作用......但为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!