尝试在Xamarin获取位置时,任务被取消

时间:2016-10-24 12:09:36

标签: c#-4.0 xamarin.forms

我正在使用zxing.net.mobile软件包进行扫描,但有时我会收到“任务被取消”这样的错误。即TaskCanceledException。我不知道如何解决它。我使用以下代码。

zxing.OnScanResult += (result) =>
{
    zxing.IsScanning = false;

    Device.BeginInvokeOnMainThread(async () =>
    {                   
        zxing.IsAnalyzing = false;

        comingIn_goingOut obj = new comingIn_goingOut();

        try
        {
            var locator = CrossGeolocator.Current;

            locator.DesiredAccuracy = 50;

            var position = await locator.GetPositionAsync(timeoutMilliseconds: 3000);

            obj.empId = Resources.LIVE_USER_ID;
            obj.qrValue = result.Text;
            obj.latitude = position.Latitude.ToString();
            obj.longitude = position.Longitude.ToString();
        }
        catch (Exception ex)
        {
            await DisplayAlert ("Alert", ex.ToString(), "Ok");
        }
        try
        {
            var json = JsonConvert.SerializeObject(obj);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            HttpResponseMessage response
                = await client.PostAsync(Resources.baseUrl + "goingout", content);

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<string> (responseContent);
                await this.Navigation.PopModalAsync();
                await DisplayAlert("Message", res.ToString(), "OK");
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Alert", ex.ToString(), "OK");
        }
    });
};

0 个答案:

没有答案