简体   繁体   English

WCF服务返回Xamarin.Android中的对象列表

[英]WCF Service Returns List of Objects in Xamarin.Android

I want to return list of object in WCF , my problem is how do i implement it in my application 我想返回WCF中的对象列表,我的问题是如何在应用程序中实现它

public List GetConsumer(string transformer, string account) { var search = new List();

    using (var conn = new MySqlConnection(ConnString))
    {
        var item = new SearchConsumer();
        conn.Open();
        const string cmdText = "SELECT * FROM tblconsumer_account WHERE transformer=@tsf AND accountNumber=@acct";
        var cmd = new MySqlCommand(cmdText, conn);
        cmd.Parameters.AddWithValue("@tsf", transformer);
        cmd.Parameters.AddWithValue("@acct", account);
        var reader = cmd.ExecuteReader();
        while (reader.Read())
        {

            item.SearchResult = true;
            item.AccountNumber = reader["accountNumber"].ToString();
            item.TrasformerNumber = reader["transformer"].ToString();
            item.Firstname = reader["firstname"].ToString();
            item.LastName = reader["lastname"].ToString();
            item.Address = reader["address"].ToString();
            item.SerialNumber = reader["serialNumber"].ToString();
            search.Add(item);
        }
    }
    return search.ToList();
}

iam using this guide as reference, but i have a problem finding properties in my application,like e.Result.Lastname , e.Result.Firstname 我使用本指南作为参考,但是我在查找应用程序中的属性时遇到问题,例如e.Result.Lastnamee.Result.Firstname

Change the following 改变以下

public List GetConsumer(string transformer, string account)

to

public List<SearchConsumer> GetConsumer(string transformer, string account) {

and then update your service reference on your mobile project. 然后在您的移动项目上更新您的服务参考。

You will then be able to do the following: 然后,您将可以执行以下操作:

using(var service = new MyService()) //MyService is the name of your service reference
{
    var searchConsumers = service.GetConsumer(transformer, account);
    //Then loop your list
    foreach(var searchConsumer in searchConsumers)
    {
        //Now access properties
        //searchConsumer.Firstname, searchConsumer.Lastname
    }
}

If you only want to return a single object, then you will need to change your signature to the following: 如果只想返回一个对象,则需要将签名更改为以下内容:

public SearchConsumer GetConsumer(string transformer, string account)

and change the body of the function to only get one item, the usage for this would be: 并将函数主体更改为仅获得一项,则其用法为:

using(var service = new MyService()) //MyService is the name of your service reference
{
    var searchConsumer = service.GetConsumer(transformer, account);
    //Now access properties
    //searchConsumer.Firstname, searchConsumer.Lastname
}

Edit 编辑

If you are going to use async web service calls you would need to do something like this 如果您要使用异步Web服务调用,则需要执行以下操作

using(var service = new MyService()) //MyService is the name of your service reference
{
    service.GetConsumerCompleted += GetConsumerCompletedCallback;
    service.GetConsumerAsync(transformer, account);
}

//this method can be auto generated when hitting tab twice in visual studio after adding +=

public void (object sender, GetConsumerCompletedEventArgs e)
{
    //e.Result.Firstname, e.Result.Lastname
    //or if you are returning a list then loop over using code above
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM