C# 在Xamarin中调用表节URL
我想显示两部分tableviewcontroller。我需要调用两个URL来获取json对象,并在tableviewcontroller中将它们制成表格。我不知道如何创建一个表源来处理列表的两个不同实例 我在这里张贴完整的源代码在这里。如果有人能帮我解决这个问题,我会很高兴,或者分享一个有用的链接,或者分享代码 返回“未设置对象引用”错误。在我看来,一旦我从第一个url中输入数据并尝试将tableview制成表格,那么当时的其他url数据可能还没有准备好C# 在Xamarin中调用表节URL,c#,xamarin,C#,Xamarin,我想显示两部分tableviewcontroller。我需要调用两个URL来获取json对象,并在tableviewcontroller中将它们制成表格。我不知道如何创建一个表源来处理列表的两个不同实例 我在这里张贴完整的源代码在这里。如果有人能帮我解决这个问题,我会很高兴,或者分享一个有用的链接,或者分享代码 返回“未设置对象引用”错误。在我看来,一旦我从第一个url中输入数据并尝试将tableview制成表格,那么当时的其他url数据可能还没有准备好 命名空间TPM { 分部类IViewCo
命名空间TPM
{
分部类IViewController:UIViewController
{
公开名单;
公开名单;
公共InboxViewController(IntPtr句柄):基本(句柄)
{
this.Title=“Inside”;
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
GInbox();
CInbox();
}
公共密码箱()
{
var client=新的RestClient(“URL”);
client.Authenticator=新的HttpBasicAuthenticator(“admin”、“admin”);
var请求=新的重新请求(“其他部分URL”);
request.AddHeader(“接受”、“应用程序/json”);
request.AddHeader(“内容类型”、“应用程序/json”);
client.ExecuteAsync(请求、响应=>{
cTasks=Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content);
InvokeOnMainThread(()=>{
TableView.Source=newtablesource(cTasks,this,0);
TableView.ReloadData();
});
});
}
公用信箱()
{
var client=新的RestClient(“URL”);
client.Authenticator=新的HttpBasicAuthenticator(“admin”、“admin”);
var请求=新的重新请求(“剩余URL”);
request.AddHeader(“接受”、“应用程序/json”);
request.AddHeader(“内容类型”、“应用程序/json”);
client.ExecuteAsync(请求、响应=>{
aTasks=Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content);
InvokeOnMainThread(()=>{
Source=newtablesource(aTasks,this,1);
TableView.ReloadData();
});
});
}
公共类表源:UITableViewSource{
列表项;
列表项;
int defi;
字符串cellIdentifier=“TableCell”;
私人IViewController iv;
公共表源(Listitems、IViewController vc、int def)
{
如果(def==0)
{
cTableItems=项目;
}
否则如果(def==1)
{
aTableItems=项目;
}
iv=vc;
defi=def;
}
公共覆盖Nin NumberOfSections(UITableView tableView)
{
返回2;
}
公共覆盖nfloat GetHeightForHeader(UITableView表格视图,第九节)
{
如果(节==0){
返回40;
}
如果(节==1){
返回40;
}
返回40;
}
公共覆盖UIView GetViewForHeader(UITableView表格视图,第九节)
{
UIView headerView=新UIView(新矩形F(0,0,(浮点)UIScreen.MainScreen.Bounds.Width,(浮点)tableView.SectionHeaderHeight));
headerView.BackgroundColor=UIColor.Black;
UILabel sectionTitle=新的UILabel(新的矩形F(10,(浮动)((headerView.Frame.Height-22)/2),200,24));
sectionTitle.Font=UIFont.BoldSystemFontOfSize(22);
sectionTitle.TextColor=UIColor.White;
sectionTitle.TextAlignment=UITextAlignment.Right;
如果(节==0){
sectionTitle.Text=“Cmed”;
}
else if(节==1){
sectionTitle.Text=“Asy”;
}
headerView.AddSubview(节标题);
返回headerView;
}
公共覆盖第九行第九节(UITableView表格视图,第九节)
{
如果(节==0)
返回cTableItems。计数;
其他的
返回aTableItems。计数;
}
公共覆盖UITableViewCell GetCell(UITababVIEW TabLVIEW,基础.NSIXDEXPATH索引路径)
{
UITableViewCell cell=tableView.DequeueReusableCell(cellIdentifier);
if(单元格==null)
单元格=新的UITableViewCell(UITableViewCellStyle.Subtitle,cellIdentifier);
if(indexPath.Section==0){
cell.TextLabel.Text=cTableItems[indexath.Row].displayName;
cell.DetailTextLabel.Lines=3;
cell.DetailTextLabel.Text=“进程ID:”+cTableItems[indexath.Row].processInstanceId+“\n”+DateTime.Parse(Convert.ToDateTime(cTableItems[indexath.Row].createdOn.ToShortTimeString());
if(cTableItems[indexath.Row].priority==0){
cell.ImageView.Image=UIImage.FromFile(“Images/green.png”);
}
else if(cTableItems[indexath.Row]。优先级==1){
cell.ImageView.Image=UIImage.FromFile(“Images/yellow.png”);
}
else if(cTableItems[indexath.Row]。优先级==2){
cell.ImageView.Image=UII
public override nint RowsInSection(UITableView tableview, nint section)
namespace TPM
{
partial class IViewController : UIViewController
{
public List<HumanTask> cTasks;
public List<HumanTask> aTasks;
public InboxViewController (IntPtr handle) : base (handle)
{
this.Title = "Inside";
}
public override void ViewDidLoad()
{
base.ViewDidLoad ();
GInbox ();
CInbox ();
}
public void CInbox()
{
var client = new RestClient ("URL");
client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
var request = new RestRequest ("other part URL");
request.AddHeader ("Accept", "application/json");
request.AddHeader ("Content-Type", "application/json");
client.ExecuteAsync (request, response => {
cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);
InvokeOnMainThread (() => {
TableView.Source= new TableSource(cTasks,this,0);
TableView.ReloadData();
});
});
}
public void GInbox()
{
var client = new RestClient ("URL");
client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
var request = new RestRequest ("the rest URL");
request.AddHeader ("Accept", "application/json");
request.AddHeader ("Content-Type", "application/json");
client.ExecuteAsync (request, response => {
aTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);
InvokeOnMainThread (() => {
TableView.Source= new TableSource(aTasks,this,1);
TableView.ReloadData();
});
});
}
public class TableSource:UITableViewSource{
List<HTask>cTableItems;
List<HTask>aTableItems;
int defi;
string cellIdentifier="TableCell";
private IViewController iv;
public TableSource (List<HTask>items, IViewController vc, int def)
{
if(def==0)
{
cTableItems=items;
}
else if(def==1)
{
aTableItems=items;
}
iv=vc;
defi=def;
}
public override nint NumberOfSections (UITableView tableView)
{
return 2;
}
public override nfloat GetHeightForHeader (UITableView tableView, nint section)
{
if(section==0){
return 40;
}
if(section == 1) {
return 40;
}
return 40;
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
UIView headerView = new UIView(new RectangleF (0, 0, (float)UIScreen.MainScreen.Bounds.Width, (float)tableView.SectionHeaderHeight));
headerView.BackgroundColor = UIColor.Black;
UILabel sectionTitle = new UILabel( new RectangleF(10, (float)((headerView.Frame.Height - 22) / 2), 200, 24));
sectionTitle.Font = UIFont.BoldSystemFontOfSize(22);
sectionTitle.TextColor = UIColor.White;
sectionTitle.TextAlignment = UITextAlignment.Right;
if (section == 0) {
sectionTitle.Text = "Cmed";
}
else if (section == 1) {
sectionTitle.Text = "Asy";
}
headerView.AddSubview(sectionTitle);
return headerView;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (section == 0)
return cTableItems.Count;
else
return aTableItems.Count;
}
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);
if (cell == null)
cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);
if (indexPath.Section == 0) {
cell.TextLabel.Text = cTableItems [indexPath.Row].displayName;
cell.DetailTextLabel.Lines = 3;
cell.DetailTextLabel.Text = "Process ID:" + cTableItems [indexPath.Row].processInstanceId + "\n" + DateTime.Parse (Convert.ToDateTime (cTableItems [indexPath.Row].createdOn).ToShortTimeString ());
if (cTableItems [indexPath.Row].priority == 0) {
cell.ImageView.Image = UIImage.FromFile ("Images/green.png");
}
else if (cTableItems [indexPath.Row].priority == 1) {
cell.ImageView.Image = UIImage.FromFile ("Images/yellow.png");
}
else if (cTableItems [indexPath.Row].priority == 2) {
cell.ImageView.Image = UIImage.FromFile ("Images/red.png");
}
}
else if (indexPath.Section == 1) {
cell.TextLabel.Text = assignTableItems [indexPath.Row].displayName;
cell.DetailTextLabel.Lines = 3;
cell.DetailTextLabel.Text = "Process ID:" + aTableItems [indexPath.Row].processInstanceId + "\n" + DateTime.Parse (Convert.ToDateTime (aTableItems [indexPath.Row].createdOn).ToShortTimeString ());
if (aTableItems [indexPath.Row].priority == 0) {
cell.ImageView.Image = UIImage.FromFile ("Images/green.png");
}
else if (aTableItems [indexPath.Row].priority == 1) {
cell.ImageView.Image = UIImage.FromFile ("Images/yellow.png");
}
else if (aTableItems [indexPath.Row].priority == 2) {
cell.ImageView.Image = UIImage.FromFile ("Images/red.png");
}
}
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return cell;
}
public override nfloat GetHeightForRow (UITableView tableView, Foundation.NSIndexPath indexPath)
{
return 60;
}
}
}
}
public class TableSource:UITableViewSource{
public List<HTask> cTableItems;
public List<HTask> aTableItems;
string cellIdentifier="TableCell";
private IViewController iv;
public TableSource (IViewController vc)
{
aTableItems = new List<HTask>();
cTableItems = new List<HTask>();
iv=vc;
}
public override void ViewDidLoad()
{
base.ViewDidLoad ();
TableView.Source = new TableSource(this);
GInbox ();
CInbox ();
}
public void CInbox()
{
var client = new RestClient ("URL");
client.Authenticator = new HttpBasicAuthenticator ("admin", "admin");
var request = new RestRequest ("other part URL");
request.AddHeader ("Accept", "application/json");
request.AddHeader ("Content-Type", "application/json");
//request.Method = (string)"GET";
client.ExecuteAsync (request, response => {
cTasks = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HTask>> (response.Content);
InvokeOnMainThread (() => {
((TableSource)this.TableView.Source).cTableItems = cTasks;
TableView.ReloadData();
});
});