2014-10-16 51 views
1

我試圖從Xwindow格式中使用Simple.OData.Client從Nortwind URL獲取數據。應用程序永遠掛起。簡單的項目源代碼是在下面的鏈接:基於皮特建議1我已經更新了我的代碼:DownloadOData永遠以Xamarin格式掛起

using Simple.OData.Client; 

public async void InitializeDataService(){ 

    try { 
     mODataClient = new ODataClient("http://services.odata.org/Northwind/Northwind.svc/"); 
    } 

    catch { 
     await DisplayAlert("Error", "Connection Error", "OK", "Cancel"); 
     System.Diagnostics.Debug.WriteLine("ERROR!"); 
    } 
} 

public async void GetDataFromOdataService (string myDataClicked){ 

    try { 
     myCustomers= mODataClient.For(myDataClicked).Top(10).FindEntriesAsync().Result; 
    } 

    catch { 
     await DisplayAlert("Error", "Connection Error", "OK", "Cancel"); 
     System.Diagnostics.Debug.WriteLine("ERROR!"); 
    } 
} 

更新。仍然永遠掛起。

private ODataClient mODataClient; 
private IEnumerable <IDictionary<string,object>> myCustomers; 
public ObservableCollection <Customer>Customers { get; set;} 
public string myDataString; 

public MyDataServices (string myDataClicked) 
{ 
    Title="Customers"; 
    myDataString = myDataClicked; 
    callServices(); 
} 

public async Task callServices() 
{ 
    await InitializeDataService(); 
    await GetDataFromOdataService (myDataString); 
} 

public async Task InitializeDataService(){ 

    try { 
     mODataClient = 
      new ODataClient("http://services.odata.org/Northwind/Northwind.svc/"); 
    } 
    catch { 
     await DisplayAlert("Error", "Connection Error", "OK", "Cancel"); 
     System.Diagnostics.Debug.WriteLine("ERROR!"); 
    } 
} 

public async Task GetDataFromOdataService (string myDataClicked){ 

    try { 
     myCustomers= mODataClient.For(myDataClicked).Top(10).FindEntriesAsync().Result; 
    } 
    catch { 
     await DisplayAlert("Error", "Connection Error", "OK", "Cancel"); 
     System.Diagnostics.Debug.WriteLine("ERROR!"); 
    } 
} 

回答

2

我懷疑這是由於吊你如何試圖調用異步並使其同步。

如果你這樣做,我認爲你現在只是做這個測試作爲測試,而且以後你會擴展到目前爲止你的變量只是該函數的局部變量。

所以嘗試做public static async Page GetMainPage(),並改變: -

var customers= await mODataClient.For("Customers").Top(10).FindEntriesAsync(); 

,因爲它是最有可能掛在你以前的實現。

注意你很可能不得不做異步聲明,並放在一個等待來調用GetMainPage()函數也讓這一切工作的頁面。

真的 - 雖然 - 我會把它作爲一個任務<>函數,並在您正在顯示的頁面內等待它。

更新1: -

問題涉及異步/等待問題。

數據任務被分離成一個單獨的函數返回一個任務<>,隨後更新ListView.ItemsSource控制中,當數據被檢索。

+0

Hello Pete。看來我錯過了一些不允許我的應用程序運行的小問題。我通過電子郵件向您發送了我的源代碼。 – casillas 2014-10-16 16:06:56

+0

你好皮特,我根據你的建議更新了我的代碼,它仍然永遠掛起。 – casillas 2014-10-16 16:21:15

+1

我剛剛給你發了一封修復電子郵件。這個對我有用。試着讓我知道? – Pete 2014-10-16 16:39:10

1

Pete懷疑,應用程序掛起的可能性很大,因爲您試圖同步運行它。檢查源代碼並修改您調用.Result或.Wait()的所有地方。那麼它應該工作。我知道這樣的重構可能需要大量的代碼重寫,但這是要走的路。