OData永远以Xamarin形式挂起 [英] OData Hangs Forever in Xamarin Form

查看:75
本文介绍了OData永远以Xamarin形式挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch data from the Nortwind URL with using Simple.OData.Client in Xamarin form. Application hangs forever. The simple project source code is in the following link: 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的建议更新了代码.仍然永远挂着.

Update:1 I have updated my code based on Pete suggestion. Still hangs forever.

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

推荐答案

我怀疑这是由于您尝试调用 async 并使它同步而导致的.

I suspect this is hanging due how your trying to call async and making it synchronous.

在执行此操作时,我认为您目前只是在做此测试,稍后您将对其进行扩展,因为目前您的变量仅在该函数中是本地的.

Where you are doing this, I take you are just doing this as a test at present, and that later on you will expand upon as at present your variable is only local to that function.

因此请尝试执行public static async Page GetMainPage(),然后更改为:-

So try doing public static async Page GetMainPage(), and changing to:-

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

,因为它很可能会挂在您以前的实现中.

as it is most likely hanging on your previous implementation.

请注意,您很可能必须做 async 声明,并在调用 GetMainPage()函数的页面上进行 await 使所有这些正常工作.

Note you will most likely have to do async declaration and put in an await to the page that calls the GetMainPage() function also to get all this working.

确实-尽管-我会将其作为一个Task<>函数,并在要显示的页面中等待它.

Really - though - I would take this as a Task<> function and await it from within the Page that you are displaying.

更新1:-

问题与异步/等待问题有关.

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

Data task was separated out into a separate function returning a Task<>, subsequently updating the ListView.ItemsSource control when the data is retrieved.

这篇关于OData永远以Xamarin形式挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆