C# 在android应用程序中调用wcf并传递listview所选项目
我知道前几天我发布了一个问题,关于如何将选定的android listview项传递给wcf服务应用程序。我只是想用最简单的方法让事情顺利进行。我遇到了使用wcf从所选县获取城镇的问题。我有一张带有id和county的county表。在towns表中,我有id、姓名和countyid 以下是我的wcf服务应用程序:C# 在android应用程序中调用wcf并传递listview所选项目,c#,android,wcf,xamarin,C#,Android,Wcf,Xamarin,我知道前几天我发布了一个问题,关于如何将选定的android listview项传递给wcf服务应用程序。我只是想用最简单的方法让事情顺利进行。我遇到了使用wcf从所选县获取城镇的问题。我有一张带有id和county的county表。在towns表中,我有id、姓名和countyid 以下是我的wcf服务应用程序: [OperationContract] Festivalwrapper GetTownDataByCounty(int? id); public Festivalwrapper G
[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
我的第二项活动是:
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
[活动(主题=“@style/Theme.AppCompat.Light”)]
公共类SelectLocationActivity:活动
{
私有DataTransferProcClient\u客户端;
TextView_getdownstextview;
ListView _ListView;
公共静态只读EndpointAddress EndPoint=新的EndpointAddress(“http://10.0.2.2:3190/DataTransferProc.svc");
//私有int-id;
创建时受保护的覆盖无效(捆绑包)
{
//var id=Intent.GetStringExtra(“位置”);
base.OnCreate(bundle);
SetContentView(Resource.Layout.selectLocation);
_listView=FindViewById(Resource.Id.lvTowns);
//_listView.OnItemClickListener=此;
_listView.FastScrollEnabled=true;
_getTownsTextView=findviewbyd(Resource.Id.getTownsTextView);
初始化为tatownbyid();
}
#区域初始化为Statown
private void InitializeDataownbyid()
{
var id=Intent.GetStringExtra(“名称”);
int值;
int.TryParse(id,out值);
如果(id!=null)
{
BasicHttpBinding=CreateBasicHttp();
_客户端=新的DataTransferProcClient(绑定,端点);
_client.GetTownDataByCountyCompleted+=ClientOnDataTransferProcCompleted;
_client.GetTownDataByCountyAsync(id);
}
//_client.Close();
}
#端区
#区域CreateBasicHttp
私有静态BasicHttpBinding CreateBasicHttp()
{
var binding=new BasicHttpBinding()
{
Name=“basicHttpBinding”,
MaxReceivedMessageSize=67108864,
};
binding.ReaderQuotas=new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength=2147483646,
MaxStringContentLength=5242880,
};
var超时=新的时间跨度(0,1,0);
binding.SendTimeout=超时;
binding.OpenTimeout=超时;
binding.ReceiveTimeout=超时;
返回绑定;
}
#端区
#已完成区域Clientondataransferproc
//测试与wcf的连接,如果不起作用,您将得到一个错误
私有void ClientOnDataTransferProcCompleted(对象发送方,GetTownDataByCountyCompletedEventArgs GetTownDataByCountyCompletedEventArgs)
{
字符串msg=null;
if(getTownDataByCountyCompletedEventArgs.Error!=null)
{
msg=getTownDataByCountyCompletedEventArgs.Error.Message;
msg+=getTownDataByCountyCompletedEventArgs.Error.InnerException;
RunOnUiThread(()=>_getTownsTextView.Text=msg);
}
else if(getTownDataByCountyCompletedEventArgs.Cancelled)
{
msg=“请求已取消。”;
RunOnUiThread(()=>_getTownsTextView.Text=msg);
}
其他的
{
msg=getTownDataByCountyCompletedEventArgs.Result.ToString();
TestAndroid.Festivalwrapper testHolder=getTownDataByCountyCompletedEventArgs.Result;
List holder=testHolder.TownList.Select(item=>item.Name.ToList();
/*foreach(TestHolder.CountyList中的TestAndroid.CountyVM项)
{
持有人。添加(项目名称);
}*/
RunOnUiThread(()=>_listView.Adapter=new ArrayAdapter(这个,Android.Resource.Layout.SimpleListItem1,holder));
}
}
#端区
我在将字符串转换为int时也遇到问题。我在以下位置遇到错误:_client.GetTownDataByCountyAsync(id)
啊,我明白了,但除此之外,我在做其他事情对吗@Jason?再次查看代码后,我发现您使用的是TryParse(),但在调用_client.GetTownDataByCountyAsync(id)时,您传递的是输入(id)而不是输出(值);谢谢Jason,我成功了。唯一的问题是,我的listview从0开始。有没有办法让listview从1开始?