【问题标题】:OData Hangs Forever in Xamarin FormOData 以 Xamarin 形式永远挂起
【发布时间】:2015-05-20 06:19:46
【问题描述】:

我正在尝试使用 Xamarin 形式的 Simple.OData.Client 从 Nortwind URL 获取数据。应用程序永远挂起。简单的项目源码在以下链接:Download

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!");
    }
}

更新:1 我已根据 Pete 的建议更新了我的代码。仍然永远挂起。

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!");
    }
}

【问题讨论】:

    标签: xamarin odata xamarin.forms


    【解决方案1】:

    我怀疑这是挂起的,因为您尝试调用 async 并使其同步。

    您在哪里执行此操作,我认为您目前只是将其作为测试,稍后您将对此进行扩展,因为目前您的变量仅适用于该函数。

    所以尝试做public static async Page GetMainPage(),然后改为:-

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

    因为它很可能会挂在您之前的实现上。

    请注意,您很可能必须进行 async 声明,并将 await 放入调用 GetMainPage() 函数的页面让所有这些工作。

    真的 - 不过 - 我会将此作为一个任务 函数并在您正在显示的页面中等待它。

    更新 1:-

    问题与 async / await 问题有关。

    数据任务被分离到一个单独的函数中,返回一个 Task,随后在检索数据时更新 ListView.ItemsSource 控件。

    【讨论】:

    • 你好皮特。似乎我遗漏了一些不允许我的应用运行的小东西。我已通过电子邮件向您发送了我的源代码。
    • 你好Pete,我已经根据你的建议更新了我的代码,它仍然永远挂起。
    • 我刚刚向您发送了一封包含修复程序的电子邮件。这个对我有用。试试告诉我?
    【解决方案2】:

    正如 Pete 所怀疑的,应用程序挂起很可能是因为您尝试同步运行它。检查源代码并修改所有调用 .Result 或 .Wait() 的地方。然后它应该工作。我知道这样的重构可能需要大量的代码重写,但这是要走的路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      相关资源
      最近更新 更多