【发布时间】: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);
- 错误 CS1503:参数 1:无法从 'string' 转换为 'int' (CS1503)
- 错误 CS1502:匹配“DataTransferProcClient.GetTownDataByCountyAsync(int)”的最佳重载方法包含一些无效参数 (CS1502)
【问题讨论】: