我已经从我的 mac mini 设备的 Visual Studio(ios 13)创建了一个项目 xamarin.ios。 我已经删除了故事板文件。
我有 Main.cs、AppDelegate.cs、TableView.cs 和 TableSource.cs 文件。
主要.cs:
using System;
using UIKit;
namespace LabelWrap
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
//UIApplication.Main(args, null, typeof(AppDelegate));
try
{
UIApplication.Main(args, null, "AppDelegate");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
AppDelegate.cs:
using System;
using Foundation;
using UIKit;
namespace LabelWrap
{
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.MakeKeyAndVisible();
window.RootViewController = new TableView();
return true;
}
}
}
TableSource.cs:
using System;
using Foundation;
using UIKit;
namespace LabelWrap
{
public class TableSource : UITableViewSource
{
string[] TableItems;
string CellIdentifier = "TableCell";
public TableSource(string[] items)
{
TableItems = items;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return TableItems.Length;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
string item = TableItems[indexPath.Row];
//if there are no cells to reuse, create a new one
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
}
cell.TextLabel.Text = item;
return cell;
}
}
}
TableView.cs:
using Foundation;
using UIKit;
namespace LabelWrap
{
public partial class TableView : UITableViewController
{
UITableView table;
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.Green;
table = new UITableView(View.Bounds); // defaults to Plain style
string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
table.Source = new TableSource(tableItems);
table.BackgroundColor = UIColor.Yellow;
Add(table);
}
}
}
我的项目运行成功,但当我打开应用程序时,Ios模拟器(ios 13)黑屏。 当我在代码中放置断点时,似乎一切都正确执行,但我在屏幕上看不到任何差异。
我是否遗漏了代码中的某些内容或者有错误?这样我在tableView中写的结果就可以在屏幕上看到了