我正在尝试将REST API中的JSON转换为Xamarin.iOS。虽然我能够从API获取数据,但不知道在TableView中没有填充数据。
视图模型
public class SchoolSelectionViewModel : BaseViewModel
{
private readonly ISchoolNames _schoolService;
public SchoolSelectionViewModel(ISchoolNames schoolService)
{
_schoolService = schoolService;
}
public override void Start()
{
IsLoading = true;
_schoolService.GetFeedItems(OnDilbertItems, OnError);
}
private void OnDilbertItems(List<SchoolModel> list)
{
IsLoading = false;
Items = list;
}
private void OnError(Exception error)
{
// not reported for now
IsLoading = false;
}
private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set { _isLoading = value; RaisePropertyChanged(() => IsLoading); }
}
private List<SchoolModel> _items;
public List<SchoolModel> Items
{
get { return _items; }
set { _items = value; RaisePropertyChanged(() => Items); }
}
private MvvmCross.Core.ViewModels.MvxCommand<SchoolModel> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new MvvmCross.Core.ViewModels.MvxCommand<SchoolModel>(DoSelectItem);
return _itemSelectedCommand;
}
}
private void DoSelectItem(SchoolModel item)
{
//ShowViewModel<DetailViewModel>(item);
}
}
服务
public class SchoolNames : ISchoolNames
{
public SchoolNames()
{
}
public void GetFeedItems(Action<List<SchoolModel>> success, Action<Exception> error)
{
var url = "http://demo.imanage-school.com/api/configurations/schoolnames";
var request = (HttpWebRequest)WebRequest.Create(url);
try
{
request.BeginGetResponse(result => ProcessResponse(success, error, request, result), null);
}
catch (Exception exception)
{
error(exception);
}
}
private void ProcessResponse(Action<List<SchoolModel>> success, Action<Exception> error, HttpWebRequest request, IAsyncResult result)
{
try
{
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
var json_data = reader.ReadToEnd();
json_data = json_data.Replace("\"", string.Empty);
json_data = json_data.Replace("\\", "\"");
//if (text.StartsWith("\""))
//{
// text = text.Remove(0, 1);
//}
//if (text.EndsWith("\""))
//{
// text = text.Remove(text.Length-1, 1);
//}
List<SchoolModel> List = new List<SchoolModel>();
//List = JsonConvert.DeserializeObject < List <SchoolModel>> text;
//List = JsonConvert.DeserializeObject(< List < SchoolModel >> text);
var objects = JsonConvert.DeserializeObject<List<SchoolModel>>(json_data);
success(List);
}
}
catch (Exception exception)
{
error(exception);
}
}
private List<SchoolModel> ParseDilbertItemList(string text)
{
var root = JsonConvert.DeserializeObject<SchoolModel>(text);
var jsondetails = JsonConvert.SerializeObject(text);
var xml = XDocument.Parse(text);
var items = xml.Descendants("item");
var list = items.Select(x =>
new SchoolModel()
{
Name = x.Element("title").Value,
ApiUrl = ExtractHttpImg(x.Element("description").Value)
}).ToList();
return list;
}
private string ExtractHttpImg(string value)
{
var startPos = value.IndexOf("http://");
var endPos = value.IndexOf(".gif\"", startPos);
return value.Substring(startPos, endPos + 4 - startPos);
}
}
的ViewController
public partial class SchoolSelectionView : MvxViewController<SchoolSelectionViewModel>
{
public SchoolSelectionView() : base("SchoolSelectionView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.NavigationController.SetNavigationBarHidden(true,true);
var source = new MvxStandardTableViewSource(tblSchoolSelection, "TitleText Name");
this.CreateBinding(source).To<SchoolSelectionViewModel>(vm => vm.Items).Apply();
this.CreateBinding(source).For(s => s.SelectionChangedCommand).To<SchoolSelectionViewModel>(vm => vm.ItemSelectedCommand).Apply();
tblSchoolSelection.Source = source;
tblSchoolSelection.ReloadData();
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
我觉得绑定存在一些问题,但我无法修复它。
答案 0 :(得分:0)
在服务:
中import UIKit
var timer = Timer()
var intCount = 0
var seconds = 0
let greenColor = UIColor(red: 0/255.0, green: 255/255.0, blue: 0/255.0, alpha: 1.0)
let redColor = UIColor(red: 255/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
// Start Button
@IBAction func Start(_ sender: Any)
{
repeat
{
performTimer()
intCount += 1
}while intCount < 2
StartHide.isHidden = true
}
func performTimer()
{
timer1() // Execute -> Finish
timer2() // Execute -> Finish
}
func timer1()
{
// Set Seconds
seconds = 30
// Colour for 30 Seconds
view.backgroundColor = redColor
// Run Timer
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerPage.counter), userInfo: nil, repeats: true)
}
func timer2()
{
// Set Seconds
seconds = 10
// Colour for 10 Seconds
view.backgroundColor = greenColor
// Run Timer
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerPage.counter), userInfo: nil, repeats: true)
}
func counter()
{
seconds -= 1
if (seconds == 0)
{
timer.invalidate()
}
}
首先创建变量List<SchoolModel> List = new List<SchoolModel>();
//List = JsonConvert.DeserializeObject < List <SchoolModel>> text;
//List = JsonConvert.DeserializeObject(< List < SchoolModel >> text);
var objects = JsonConvert.DeserializeObject<List<SchoolModel>>(json_data);
success(List);
,然后反序列化JSON,但将其存储到新变量List
中。但是,您然后将objects
传递给List
方法,但success
实际上是空的。您可能希望以List
结束以使其正常工作。