Xamarin.iOS应用运行后iOS 13模拟器黑屏问题排查求助
Xamarin.iOS(iOS 13)项目删除Storyboard后黑屏问题排查
问题背景及代码
我在Mac mini设备上通过Visual Studio创建了Xamarin.iOS(iOS 13)项目,并删除了Storyboard文件,现有Main.cs、AppDelegate.cs、TableView.cs和TableSource.cs四个文件,代码如下:
Main.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 13模拟器打开应用后显示黑屏,断点调试显示所有代码均正常执行,却看不到TableView的内容。请问我是否遗漏了什么或者代码存在错误?
问题原因及修复方案
问题出在TableView类的实现上:你继承了UITableViewController,这个类本身已经内置了一个TableView属性,不需要再手动创建新的UITableView实例并添加到视图中,手动创建的操作会和控制器自带的TableView产生冲突,导致界面无法正常显示。
修复后的TableView.cs代码
using Foundation; using UIKit; namespace LabelWrap { public partial class TableView : UITableViewController { public override void ViewDidLoad() { base.ViewDidLoad(); View.BackgroundColor = UIColor.Green; string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" }; // 使用UITableViewController自带的TableView属性 TableView.Source = new TableSource(tableItems); TableView.BackgroundColor = UIColor.Yellow; } } }
额外优化建议
在AppDelegate中,建议先设置RootViewController再调用window.MakeKeyAndVisible(),这是iOS开发的规范写法,虽然不是黑屏的直接原因,但能避免潜在的生命周期问题:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { window = new UIWindow(UIScreen.MainScreen.Bounds); // 先设置根控制器 window.RootViewController = new TableView(); // 再将窗口设为可见并激活 window.MakeKeyAndVisible(); return true; }
内容的提问来源于stack exchange,提问作者DimitrisRammos
相关产品推荐
相关产品推荐

