【发布时间】:2015-09-30 19:08:01
【问题描述】:
在 Xamarin Forms 文件中,我有一个带有方法的接口:GetCurrentLocation,以及两个属性(均为字符串):纬度和经度。
在 Xamarin iOS 文件中,我有一个来自前面提到的接口的类接口。该方法(现在详细说明)指出,在激活后,它会创建一个 MKMapView,获取用户的纬度。和 long.,并将它们设置为等于 lat。并且很长。类的属性(如下图)。
public string latitude { get; set; }
public string longitude { get; set; }
public void GetCurrentLocation () {
var vm = new MKMapView();
latitude = mv.UserLocation.Coordinate.Latitude.ToString();
longitude = mv.UserLocation.Coordinate.Longitude.ToString();
}
对于这个例子,这个方法只会被调用一次,目的只是为了得到纬度。并且很长。设备并以文本形式显示。
(经过一些依赖服务恶作剧......)回到 Xamarin Forms 文件中,我有一个页面,我在其中调用方法来获取位置,然后使用新获取的坐标以文本形式显示设备的当前位置. (顺便说一句,我确实在模拟器中设置了自定义位置。)
现在,我的问题是当我实际运行程序时,显示的字符串表示 lat.并且很长。都是 0。(“纬度 = 0,经度 = 0”)WHYYYYYYYYYYY?!?!? ??? 最后一张悲伤的脸
更新:这是我在 NuGet 包中添加后的代码 ---
首先 - IMapView(接口) - 包括以下内容:
public interface IMapView {
double latitude { get; set; }
double longitude { get; set; }
void GetLatandLong ();
IGeolocator locator { get; set; }
Position position { get; set; }
};
第二个 - MapView_iOS(原生类) - 包括以下内容:
//Obvious instantiations of the Interfaced Properties...
public async void GetLatandLong () {
locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100;
position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
latitude = Convert.ToDouble (position.Latitude);
longitude = Convert.ToDouble (position.Longitude);
}
第三个 - App.cs(x.forms 类) - 包括以下内容:
//Among other basic startup stuff...
protected override void OnStart()
{
DependencyService.Get<IMapView> ().GetLatandLong();
}
第四个也是最后一个 - ContentPage(x.forms 类) - 包含:
//Outside of the constructor:
protected override void OnAppearing () {
DisplayAlert ("It Works!", DependencyService.Get<IMapView> ().latitude + " + " + DependencyService.Get<IMapView> ().longitude, "Okay");
}
//Inside of the constructor:
var LabelGest = new TapGestureRecognizer();
LabelGest.Tapped += (s, e) => {
DisplayAlert ("It Works!", DependencyService.Get<IMapView> ().latitude + " + " + DependencyService.Get<IMapView> ().longitude, "Okay");
};
var MyLabel = new Label {
Text = "My Label",
GestureRecognizers = {LabelGest}
};
所以您可以看到我在两个不同的地方设置了显示坐标的 DisplayAlert:一次是我打开页面时,一次是我点击该页面上的某个项目时。当我点击该项目时,DisplayAlert 会正确激活并提供正确的坐标。然而,第一个 DisplayAlert(当页面出现时激活的那个)不正确地关闭。它在正确的时间弹出,但它总是给出“0 + 0”的消息。 :( 这是我的问题最简单的例子。我在一个单独的页面上也有一个地图,如下所示:
Content = new Map (MapSpan.FromCenterAndRadius (new Xamarin.Forms.Maps.Position
(DependencyService.Get<IMapView> ().latitude, DependencyService.Get<IMapView> ().longitude),
Distance.FromMiles (0.3))) {
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand
}
这会呈现类似的结果,地图上的开场位置为“0, 0”。任何帮助表示赞赏!
【问题讨论】: