我尝试了 GPS 教程,但收到以下错误消息。似乎有什么问题?
http://docs.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location
使用系统;
使用Android.App;
使用Android.Content;
使用 Android.Runtime;
使用 Android.Views;
使用 Android.Widget;
使用Android.OS;
//--- 添加了这些
使用 Android.Locations;
使用 System.Collections.Generic;
使用 System.Threading;
使用 System.Text;
命名空间 GetLocation
{
[活动(标签=“获取位置”,MainLauncher = true,图标=“@drawable/icon”)]
公共类 Activity1:活动,ILocationListener
{
//int 计数 = 1;
私人位置_currentLocation;
私人位置管理器_位置管理器;
私有TextView _locationText;
私有TextView _addressText;
protected override void OnCreate(捆绑包)
{
base.OnCreate(捆绑);
SetContentView(Resource.Layout.Main);
_addressText = FindViewById(Resource.Id.address_text);
_locationText = FindViewById(Resource.Id.location_text);
FindViewById(Resource.Id.get_address_button).Click += AddressButton_OnClick;
初始化位置管理器();
}
//public void OnLocationChanged(位置位置) {}
公共无效 OnProviderDisabled(字符串提供者){}
公共无效 OnProviderEnabled(字符串提供者){}
public void OnStatusChanged(string provider, Availability status, Bundle extras) {}
私人无效 InitializeLocationManager()
{
_locationManager = (LocationManager) GetSystemService(LocationService);
var criteriaForLocationService = 新标准
{
准确度 = 准确度. 精细
};
var 可接受的LocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);
if (acceptableLocationProviders.Any())
{
_locationProvider = 可接受的LocationProviders.First();
}
别的
{
_locationProvider = String.Empty;
}
}
受保护的覆盖无效 OnResume()
{
base.OnResume();
_locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
}
受保护的覆盖无效 OnPause()
{
base.OnPause();
_locationManager.RemoveUpdates(this);
}
private void AddressButton_OnClick(对象发送者,EventArgs eventArgs)
{
如果(_currentLocation == null)
{
_addressText.Text = "无法确定当前位置。";
返回;
}
新线程(()=>
{
var addressText = "找不到位置。";
var geocoder = new Geocoder(this);
var addressList = geocoder.GetFromLocation(_currentLocation.Latitude, _currentLocation.Longitude, 50);
var address = addressList.FirstOrDefault();
如果(地址!= null)
{
var deviceLocation = new StringBuilder();
for (var i = 0; i { _addressText.Text = addressText; });
})。开始();
}
public void OnLocationChanged(位置位置)
{
_currentLocation = 位置;
如果(_currentLocation == null)
{
_locationText.Text = "无法确定您的位置。";
}
别的
{
_locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude);
}
}
}
}
如何解决这些问题:
错误信息
1)
错误 CS1061:“System.Collections.Generic.IList”不包含“Any”的定义,并且找不到接受“System.Collections.Generic.IList”类型的第一个参数的扩展方法“Any”(您是否缺少using 指令还是程序集引用?)(CS1061)(GetLocation)
2)Error CS0103: The name '_locationProvider' does not exist in the current context (CS0103) (GetLocation)
3)Error CS1061: 'System.Collections.Generic.IList<string>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.IList<string>' could be found (are you missing a using directive or an assembly reference?) (CS1061) (GetLocation)
4)Error CS0103: The name '_locationProvider' does not exist in the current context (CS0103) (GetLocation)
5)Error CS1061: 'System.Collections.Generic.IList<Android.Locations.Address>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Collections.Generic.IList<Android.Locations.Address>' could be found (are you missing a using directive or an assembly reference?) (CS1061) (GetLocation)
6)文件名是否为 MainActivity.cs 并且调用 Activity 类是否重要:
public class Activity1 : Activity, ILocationListener
{
}
谢谢