c# - 使用 MVVM 在 Xamarin 中的 Android 中进行 GPS 位置

标签 c# android mvvm xamarin gps

我尝试在 Android 中获取当前 GPS 位置,而不使用 Xamarin MVVM 在后台进程的 UI 上显示,并且每当我进行方法调用时都无法获取它,我知道事件处理程序导致了问题,是否有任何问题单击它后立即获取它的解决方法?

可移植项目中的

代码-App.cs:

public string GetLocation(){
 loc = DependencyService.Get<IMyLocation>();
            loc.locationObtained += (object sender,
                ILocationEventArgs e) => {
                    var lat = e.lat;
                    var lng = e.lng;
                    latitude = lat.ToString();
                    longitude = lng.ToString();
                };
            loc.ObtainMyLocation();
return latitude+":"+longitude;
}

这是我的界面代码:

public interface IMyLocation
{
    void ObtainMyLocation();
    event EventHandler<ILocationEventArgs> locationObtained;
}
public interface ILocationEventArgs
{
    double lat { get; set; }
    double lng { get; set; }

}

最后是 Droid 项目上的依赖服务插件:

[assembly: Xamarin.Forms.Dependency(typeof(GetMyLocation))]
namespace Tutorial.Droid
{
    public class LocationEventArgs : EventArgs, ILocationEventArgs
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }

    public class GetMyLocation : Java.Lang.Object,
                                IMyLocation,
                                ILocationListener
    {
        LocationManager lm;
        public void OnProviderDisabled(string provider) { }
        public void OnProviderEnabled(string provider) { }
        public void OnStatusChanged(string provider,
            Availability status, Android.OS.Bundle extras)
        { }
        //---fired whenever there is a change in location---
        public void OnLocationChanged(Location location)
        {
            if (location != null)
            {
                LocationEventArgs args = new LocationEventArgs();
                args.lat = location.Latitude;
                args.lng = location.Longitude;
                locationObtained(this, args);
            };
        }
        //---an EventHandler delegate that is called when a location
        // is obtained---
        public event EventHandler<ILocationEventArgs>
            locationObtained;
        //---custom event accessor that is invoked when client
        // subscribes to the event---
        event EventHandler<ILocationEventArgs>
            IMyLocation.locationObtained
        {
            add
            {
                locationObtained += value;
            }
            remove
            {
                locationObtained -= value;
            }
        }
        //---method to call to start getting location---
        public void ObtainMyLocation()
        {
            lm = (LocationManager)
                Forms.Context.GetSystemService(
                    Context.LocationService);
            lm.RequestLocationUpdates(
                LocationManager.NetworkProvider,
                    0,   //---time in ms---
                    0,   //---distance in metres---
                    this);
        }
        //---stop the location update when the object is set to
        // null---
        ~GetMyLocation()
        {
            lm.RemoveUpdates(this);
        }
    }
}

最佳答案

希望这个项目能够解释您的需求https://github.com/raechten/TestGPS ...我不是它的作者,只是在冲浪时发现的

关于c# - 使用 MVVM 在 Xamarin 中的 Android 中进行 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34669162/

相关文章:

c# - EF Core - 可能导致循环或多个级联路径

java - 尽管组件的 ID 存在,findViewById 返回 null

android - SASLError 使用 PLAIN : not-authorized

c# - WPF MVVM 检索数据网格选定的行

c# - 用枚举参数化对象

c# - 具有自定义用户控件的多阶段数据绑定(bind)

c# - 根据以前的值实时猜测值

c# - 读取 RSS 源中的特定标签

c# - xamarin 中的警告 XA4211

android - 当 MediaPlayer 在不同的位置播放时,如何从只读的 RandomAccessFile 中读取