将来自 ASP.NET 网络服务的数据绑定到 Xamarin 中的微调器
Binding data from ASP.NET webservice to a spinner in Xamarin
我目前正在试用 Xamarin Free 并尝试连接我的网络服务 (ASP.NET) 以填充微调器。
我想做的是异步调用服务,然后在返回结果后填充 Spinner。
结果是,只有一个项目被绑定 - 结果数组中的第一个元素。
如有任何建议,我们将不胜感激!
代码如下:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using WebServiceTestApplication.ServiceProxy;
namespace WebServiceTestApplication
{
[Activity (Label = "WebServiceTestApplication", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
Tablet_Services proxy;
Spinner spCategories;
ArrayAdapter<string> spCategoriesContent;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
proxy = new ServiceProxy.Tablet_Services();
spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spCategories = (Spinner)FindViewById (Resource.Id.spCategories);
spCategories.Adapter = spCategoriesContent;
UpdateTreatmentCategories ();
}
void PopulateResults (srTreatmentCategory[] result)
{
if (result != null)
{
foreach (srTreatmentCategory c in result)
{
Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
spCategoriesContent.Add(c.CategoryName);
}
}
}
public void UpdateTreatmentCategories ()
{
proxy.BegingetTreatmentCategories (delegate (IAsyncResult ar) {
var result = proxy.EndgetTreatmentCategories (ar);
PopulateResults (result);
}, null);
}
}
}
您正在更新型号列表,但没有更新适配器。尝试用加载的数据更新适配器。
编辑
这是最简单的例子。
void PopulateResults (srTreatmentCategory[] result)
{
if (result != null)
{
foreach (srTreatmentCategory c in result)
{
Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
spCategoriesContent.Add(c.CategoryName);
}
spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spCategories.Adapter = spCategoriesContent;
}
}
最后你应该考虑用 updateData
/ swapData
方法编写更多你自己的 Adapter
实现。
对于名为“类别”的下拉列表,这是对我有用的。我认为您通过使用称为 PopulateResults() 的方法走在正确的轨道上,我将其称为 LoadSpinnerData()。
CategorySpinner = dialogView.FindViewById<Spinner>(Resource.Id.spinnerCategory);
CategorySpinner.ItemSelected += spinner_ItemSelected;
private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
SelectedCategory = string.Format("{0}", spinner.GetItemAtPosition(e.Position));
}
private void LoadSpinnerData()
{
var tempCategories = (List<ServiceCategory>) CategoryManager.GetCategories();
var categories = tempCategories.Select(category => category.Name).ToList();
var categoryAdapter = new ArrayAdapter<string>(
Activity, Android.Resource.Layout.SimpleSpinnerItem, categories);
categoryAdapter.SetDropDownViewResource
(Android.Resource.Layout.SimpleSpinnerDropDownItem);
CategorySpinner.Adapter = categoryAdapter;
}
您可以在此处找到整个代码文件https://github.com/valokafor/XamarinAndroidCustomDialog/blob/master/XamarinDroidCustomListView/ServiceDialog.cs
祝 Xamarin 好运
我目前正在试用 Xamarin Free 并尝试连接我的网络服务 (ASP.NET) 以填充微调器。
我想做的是异步调用服务,然后在返回结果后填充 Spinner。
结果是,只有一个项目被绑定 - 结果数组中的第一个元素。
如有任何建议,我们将不胜感激!
代码如下:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using WebServiceTestApplication.ServiceProxy;
namespace WebServiceTestApplication
{
[Activity (Label = "WebServiceTestApplication", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
Tablet_Services proxy;
Spinner spCategories;
ArrayAdapter<string> spCategoriesContent;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
proxy = new ServiceProxy.Tablet_Services();
spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spCategories = (Spinner)FindViewById (Resource.Id.spCategories);
spCategories.Adapter = spCategoriesContent;
UpdateTreatmentCategories ();
}
void PopulateResults (srTreatmentCategory[] result)
{
if (result != null)
{
foreach (srTreatmentCategory c in result)
{
Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
spCategoriesContent.Add(c.CategoryName);
}
}
}
public void UpdateTreatmentCategories ()
{
proxy.BegingetTreatmentCategories (delegate (IAsyncResult ar) {
var result = proxy.EndgetTreatmentCategories (ar);
PopulateResults (result);
}, null);
}
}
}
您正在更新型号列表,但没有更新适配器。尝试用加载的数据更新适配器。
编辑 这是最简单的例子。
void PopulateResults (srTreatmentCategory[] result)
{
if (result != null)
{
foreach (srTreatmentCategory c in result)
{
Console.WriteLine ("Adding {0} to spCategoriesContent", c.CategoryName);
spCategoriesContent.Add(c.CategoryName);
}
spCategoriesContent = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleSpinnerItem);
spCategoriesContent.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spCategories.Adapter = spCategoriesContent;
}
}
最后你应该考虑用 updateData
/ swapData
方法编写更多你自己的 Adapter
实现。
对于名为“类别”的下拉列表,这是对我有用的。我认为您通过使用称为 PopulateResults() 的方法走在正确的轨道上,我将其称为 LoadSpinnerData()。
CategorySpinner = dialogView.FindViewById<Spinner>(Resource.Id.spinnerCategory);
CategorySpinner.ItemSelected += spinner_ItemSelected;
private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
var spinner = (Spinner)sender;
SelectedCategory = string.Format("{0}", spinner.GetItemAtPosition(e.Position));
}
private void LoadSpinnerData()
{
var tempCategories = (List<ServiceCategory>) CategoryManager.GetCategories();
var categories = tempCategories.Select(category => category.Name).ToList();
var categoryAdapter = new ArrayAdapter<string>(
Activity, Android.Resource.Layout.SimpleSpinnerItem, categories);
categoryAdapter.SetDropDownViewResource
(Android.Resource.Layout.SimpleSpinnerDropDownItem);
CategorySpinner.Adapter = categoryAdapter;
}
您可以在此处找到整个代码文件https://github.com/valokafor/XamarinAndroidCustomDialog/blob/master/XamarinDroidCustomListView/ServiceDialog.cs
祝 Xamarin 好运