【问题标题】:Callling a wcf within android app and passing listview selected item在 android 应用程序中调用 wcf 并传递 listview 选定项
【发布时间】:2023-03-20 08:12:01
【问题描述】:

我知道我前几天发布了一个问题,关于我应该如何将选定的 android listview 项传递给 wcf 服务应用程序。我只是想使用最简单的方法来让事情正常进行。我遇到了使用 wcf 从所选县获取城镇的问题。我有一个带有 id 和县的县表。在城镇表中,我有 id、name 和countyid。

这是我的 wcf 服务应用程序:

[OperationContract]
Festivalwrapper GetTownDataByCounty(int? id);

public Festivalwrapper GetTownDataByCounty(int? id)
{
    Festivalwrapper returnType = new Festivalwrapper();
    using (azureDBDataContext c = new azureDBDataContext())
    {
        returnType.TownList = (from town in c.Towns
                               where town.CountyID.Equals(id)
                               select new TownVM()
                               {
                                   ID = town.ID,
                                   Name = town.Name,
                               }).ToList();
    }
    return returnType;
}

这是我的第一个活动OnItemClick

     public void OnItemClick(AdapterView parent, View view, int position, long id)
     {
        var selectedValue = parent.GetItemIdAtPosition(position);
        //InitializeDataTownById(int position);
        var Intent = new Intent(this, typeof(SelectLocationActivity));
        // selectedValue should already be a string but...
        Intent.PutExtra("Name", selectedValue.ToString());
        StartActivity(Intent);
     }

还有我的第二个活动:

    [Activity(Theme = "@style/Theme.AppCompat.Light")]
public class SelectLocationActivity : Activity
{
    private DataTransferProcClient _client;
    TextView _getTownsTextView;
    ListView _listView;
    public static readonly EndpointAddress EndPoint = new EndpointAddress("http://10.0.2.2:3190/DataTransferProc.svc");
    //private int id;

    protected override void OnCreate(Bundle bundle)
    {
        //var id = Intent.GetStringExtra("position");

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.selectLocation);

        _listView = FindViewById<ListView>(Resource.Id.lvTowns);
        //_listView.OnItemClickListener = this;
        _listView.FastScrollEnabled = true;

        _getTownsTextView = FindViewById<TextView>(Resource.Id.getTownsTextView);

        InitializeDataTownById();
    }

    #region InitializeDataTown
    private void InitializeDataTownById()
    {
        var id = Intent.GetStringExtra("Name");
        int value;
        int.TryParse(id, out value);

        if (id != null)
        {
            BasicHttpBinding binding = CreateBasicHttp();
            _client = new DataTransferProcClient(binding, EndPoint);
            _client.GetTownDataByCountyCompleted += ClientOnDataTransferProcCompleted;
            _client.GetTownDataByCountyAsync(id);
        }
        //_client.Close ();
    }
    #endregion

    #region CreateBasicHttp
    private static BasicHttpBinding CreateBasicHttp()
    {
        var binding = new BasicHttpBinding()
        {
            Name = "basicHttpBinding",
            MaxReceivedMessageSize = 67108864,
        };

        binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
        {
            MaxArrayLength = 2147483646,
            MaxStringContentLength = 5242880,
        };

        var timeout = new TimeSpan(0, 1, 0);
        binding.SendTimeout = timeout;
        binding.OpenTimeout = timeout;
        binding.ReceiveTimeout = timeout;
        return binding;
    }
    #endregion

    #region ClientOnDataTransferProcCompleted
    //test connection to wcf, if doesn't work, you'll get an error
    private void ClientOnDataTransferProcCompleted(object sender, GetTownDataByCountyCompletedEventArgs getTownDataByCountyCompletedEventArgs)
    {
        string msg = null;

        if (getTownDataByCountyCompletedEventArgs.Error != null)
        {
            msg = getTownDataByCountyCompletedEventArgs.Error.Message;
            msg += getTownDataByCountyCompletedEventArgs.Error.InnerException;
            RunOnUiThread(() => _getTownsTextView.Text = msg);
        }
        else if (getTownDataByCountyCompletedEventArgs.Cancelled)
        {
            msg = "Request was cancelled.";
            RunOnUiThread(() => _getTownsTextView.Text = msg);
        }
        else
        {
            msg = getTownDataByCountyCompletedEventArgs.Result.ToString();
            TestAndroid.Festivalwrapper testHolder = getTownDataByCountyCompletedEventArgs.Result;
            List<string> holder = testHolder.TownList.Select(item => item.Name).ToList();

            /*foreach (TestAndroid.CountyVM item in TestHolder.CountyList)
            {
                holder.Add (item.Name);
            }*/

            RunOnUiThread(() => _listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, holder));
        }
    }
    #endregion

我在将字符串转换为 int 时也遇到了问题。我收到错误:_client.GetTownDataByCountyAsync(id);

  1. 错误 CS1503:参数 1:无法从 'string' 转换为 'int' (CS1503)
  2. 错误 CS1502:匹配“DataTransferProcClient.GetTownDataByCountyAsync(int)”的最佳重载方法包含一些无效参数 (CS1502)

【问题讨论】:

    标签: c# android wcf xamarin


    【解决方案1】:
    string num;
    int num2;
    
    num = "2";
    
    // method 1 - will throw Exception if num is not a parsable int
    num2 = int.Parse(num);
    
    // method 2 - will not throw exception - if success is true num2 will contain the parsed int
    bool success = int.TryParse(num,out num2);
    

    【讨论】:

    • 啊,我明白了,但除此之外,我所做的一切都对吗@Jason?
    • 再次查看您的代码后,我发现您正在使用 TryParse() 但是当您调用 _client.GetTownDataByCountyAsync(id);
    • 谢谢杰森,我让它工作了。唯一的问题是,我的列表视图从 0 开始。有什么方法可以让列表视图从 1 开始?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多