Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用来自异步方法的视图上的数据_C#_Asynchronous_Xamarin_Xamarin.forms_Geolocation - Fatal编程技术网

C# 使用来自异步方法的视图上的数据

C# 使用来自异步方法的视图上的数据,c#,asynchronous,xamarin,xamarin.forms,geolocation,C#,Asynchronous,Xamarin,Xamarin.forms,Geolocation,我正在使用geolocator插件检索我的当前位置,并向页面添加pin。以下是此项服务: tasks.cs public async Task<Plugin.Geolocator.Abstractions.Position> GetDeviceCurrentLocation() { try { var locator = Plugin.Geolocator.CrossGeolocator.Current; locator.Desired

我正在使用geolocator插件检索我的当前位置,并向页面添加pin。以下是此项服务:

tasks.cs

public async Task<Plugin.Geolocator.Abstractions.Position> GetDeviceCurrentLocation()
{
    try
    {
        var locator = Plugin.Geolocator.CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;

        var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(1));

        if (position != null)
        {
            return position;
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
    }

    return new Plugin.Geolocator.Abstractions.Position();
}
公共异步任务GetDeviceCurrentLocation()
{
尝试
{
var locator=Plugin.geoloator.crossgeoloator.Current;
locator.DesiredAccuracy=50;
var position=await locator.GetPositionAsync(TimeSpan.FromSeconds(1));
如果(位置!=null)
{
返回位置;
}
}
捕获(例外情况除外)
{
WriteLine(“无法获取位置,可能需要增加超时:”+ex);
}
返回新的Plugin.geologitor.Abstractions.Position();
}
我试图在视图中使用它,如下所示:

public MapPage(List<Models.xxx> xxx, Models.yyy yyy )
{
  InitializeComponent();
  Tasks ts = new Tasks();
  var myLocation = ts.GetDeviceCurrentLocation();
  var latitudeIm = myLocation.Result.Latitude;
  var longitudeIm = myLocation.Result.Longitude;
  var pin1 = new Pin
  {
    Type = PinType.Place,
    Position = new Position(latitudeIm, longitudeIm),
    Title = "My Location"
  };
  customMap.Pins.Add(pin1);
}
公共地图页(列表xxx,Models.yyy-yyy)
{
初始化组件();
任务ts=新任务();
var myLocation=ts.GetDeviceCurrentLocation();
var latitudeIm=myLocation.Result.Latitude;
var longitudeIm=myLocation.Result.Longitude;
var pin1=新引脚
{
Type=PinType.Place,
位置=新位置(latitudeIm、longitudeIm),
Title=“我的位置”
};
customMap.Pins.Add(pin1);
}
当我尝试以下代码时,我的应用程序中断
var latitudeIm=myLocation.Result.Latitude

我想因为我有一个异步任务,所以结果必须是等待的
。你知道如何在我的视图中使用公共异步任务GetDeviceCurrentLocation()
数据吗?

你应该使用
等待
异步方法

var myLocation = await ts.GetDeviceCurrentLocation();
var latitudeIm = myLocation.Latitude;
var longitudeIm = myLocation.Longitude;
您应该将所有方法完全修饰为
async
。如果您不能应用它(我不推荐),您可以使用
ConfigureAwait
来防止死锁

var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

var myLocation = ts.GetDeviceCurrentLocation().Result;//Also don't hit the Result twice
var latitudeIm = myLocation.Latitude;
var longitudeIm = myLocation.Longitude;

我等不及了。
ts.GetDeviceCurrentLocation()
。请参见编辑。我应该创建一个模型吗?只有纬度和经度属性?你说的控制器是什么意思?我在项目中没有控制器。我没有使用MVC模式。我不能使用
var myLocation=await ts.GetDeviceCurrentLocation()
这一行,因为我试图访问页面构造函数上的数据,但它不是异步的,这就是为什么它会抛出错误的原因。如果我能用wait,问题就会解决/那么,你可以使用“ConfigureAwait”了。我试过了,但仍然是死锁,应用程序仍然会崩溃。还有别的办法吗?是否有其他方法?是否尝试删除ConfigureWait并应用Task.Run;“Task.Run(()=>ts.GetDeviceCurrentLocation()).Result;”但正如我所说,这不是一个好的实践。完全用异步修饰方法可能会更好。