2014-10-01 66 views
0

我想在加載主頁時啓動一個任務,這會在後臺使用Xamarin.Mobile獲取我的位置。難點在於等待,如果這個任務沒有完成,當用戶點擊一個按鈕時。在Windows Phone中運行異步任務並稍後等待

在Xamarin的iOS,我設法做到這一點,但是當我試圖做同樣在Windows Phone 8.0,我得到一個AggregateException與作爲消息:「一個或多個錯誤發生。」

還有就是我使用的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Shell; 
using System.Diagnostics; 
using System.IO; 
using Microsoft.Phone.Scheduler; 
using System.Threading; 
using System.Threading.Tasks; 
using System.ComponentModel; 
using Xamarin.Geolocation; 

namespace Application.WinPhone 
{ 
    public partial class Connexion : PhoneApplicationPage 
    { 
     static Task w; 

     // Constructor 
     public Connexion() 
     { 
      InitializeComponent(); 

      w = new Task (() => 
      { 
       Debug.WriteLine("Start"); 
       Geolocator geolocator = null; 

       geolocator = new Geolocator() { DesiredAccuracy = 50}; 

       var t = geolocator.GetPositionAsync(8000).ContinueWith(x => 
       { 
        Debug.WriteLine(string.Format("Latitude : {0} Longitude : {1}",  x.Result.Latitude, x.Result.Longitude)); //Visual Studio's debugger indicate this line with the exception 
       }); 
       t.Wait(); 
       Debug.WriteLine("Finished"); 
      }); 
      w.Start(); 

     } 

     private void Connexion_Click(object sender, RoutedEventArgs e) 
     { 
       w.Wait(); 

       //Here use the position find by the task to know on which page send the user 
       NavigationService.Navigate(new Uri("/Inscription.xaml", UriKind.RelativeOrAbsolute)); 
     } 
    } 
} 

如果在我的崗位對不起一些語法錯誤,我是法國人。 :)

在此先感謝您的幫助。

+0

檢查AggregateException的'InnerException'屬性以瞭解發生了什麼 – 2014-10-01 12:26:58

+0

InnerException的消息說:「任務被取消」。 爲什麼這個任務會被取消? – Nunshakin 2014-10-01 13:02:05

+0

你正在構造函數中完成這個任務,那叫什麼?也許它耗時太長,有什麼東西在殺死它? – valdetero 2014-10-01 14:18:12

回答

-1

有一件事你真正應該做的是與asnyc到rearange它/等待,你真的不需要在這種情況下創建任務,即(從我的頭):

public Connexion() 
{ 
    Connexion.IsEnabled = false; 
    var ignore = InitAsync(); 
} 
private async Task InitAsync() 
{ 
      Debug.WriteLine("Start"); 
      Geolocator geolocator = null; 

      geolocator = new Geolocator() { DesiredAccuracy = 50}; 

      var result = await geolocator.GetPositionAsync(8000); 
       Debug.WriteLine(string.Format("Latitude : {0} Longitude : {1}",  result.Latitude, result.Longitude)); //Visual Studio's debugger indicate this line with the exception 
      Connexion.IsEnabled = true; 
} 

注意按鈕應該除非行動成功,否則將被禁用。你還應該在這裏添加try/catch處理程序,你會得到更清晰的異常(也許Geolocator不能在非UI線程中創建?) 除了你實際使用的Geolocator類 - Forms Labs之一?

+1

您應該不會忽略正在初始化類型。調用者不可能真正知道實例何時可以安全使用,並且在使用不安全時不會阻止他們使用該類型。 – Servy 2014-10-01 14:41:05

+0

在這種情況下,它可以(至少在本例中) - 除非geolocator找到位置,否則不會啓用按鈕。但一般情況下,這取決於你在做什麼。 – 2014-10-01 14:50:16

+0

我嘗試了你的解決方案,但是我遇到了同樣的問題,我沒有超越這一行:await geolocator.GetPositionAsync(8000); 這是一個例外:任務被取消。 – Nunshakin 2014-10-01 14:52:33

1

首先,你不應該在構造函數中做任何繁重的工作。這是一個design flaw。其次,Windows Phone預計您的應用程序爲start in a limited amount of time,即最多10秒。因此,啓動應用程序並等待Geolocator 8秒鐘可能會花費太多時間,因此取消的任務。

你可以做的是在頁面的構造函數中創建Geolocator並獲取OnNavigatedTo事件中的位置。

+0

不幸的是,我得到了同樣的錯誤任務被取消。 – Nunshakin 2014-10-02 08:54:22