最近因為公司需要,開始學習如何用Xamarin開發 IOS,雖然主打用C#開發APP,但無論生命週期跟呼叫的function跟傳統的windows forms或ASP MVC都有相當大的差異性,有很大的一段GAP要跨過就是.
AppDelegate,程式的進入點
1
2
3
4
5
6public override bool FinishedLaunching(UIApplication app, NSDictionary options){ // 指定APP符合螢幕大小
UIWindow window = new UIWindow(UIScreen.MainScreen.Bounds);
// 指定APP Root的View,這邊Config.MainStoryboard是我為了避免每次重複一直寫一樣的code
// 所以把抓取Storyboard的程式碼整合到config,原本應該是UIStoryboard.FromName ("MainStoryboard", null)
window.RootViewController = Config.MainStoryboard.InstantiateInitialViewController () as UIViewController; // make the window visible window.MakeKeyAndVisible(); return true;}Storyboard,編輯流程的地方,只要把對應的Controller抓進來並且命名,Xamarin Studio會將Class檔案自動建好
取得Storyboard裡面的ViewController方法
1
2UIStoryboard.FromName ("MainStoryboard", null).InstantiateViewController (viewcontroller_name) as UIViewController;
IOS頁面間的流程靠的是對NavigationController的操作,以下是針對NavigationController的一些設定
```csharp
public override void ViewDidLoad ()
{
//edit control in viewdidload //NavigationBar的背景色
NavigationController.NavigationBar.BarTintColor = new UIColor(0.58f,0.761f,0.231f,1f);//字的顏色
NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes (){ ForegroundColor = UIColor.White }; //NavigationBar Button的顏色
NavigationController.NavigationBar.TintColor = UIColor.White; //設定NavigationBar的右邊按鈕為何
UIBarButtonItem barOptionIcon = new UIBarButtonItem (UIImage.FromBundle (“icon-options”), UIBarButtonItemStyle.Plain,
(sender, args) => { // button clicked NavigationController.PushViewController(Config.GetStoryboardViewController(“OptionViewController”),true); });
NavigationController.NavigationBar.TopItem.SetRightBarButtonItem (barOptionIcon,true); //OptionIcon
}
1 |
|
TableViewController的製作方式
```csharp
//製作TableSource的Class
public class TableSource : UITableViewSource
{
string cellIdentifier = “OptionsTableCell”;
public ListTABLE_ITEMS{ get; set;} //每列資料 public event EventHandler Cell_RowSelected;//給外層cell click事件 public TableSource ()
{
TABLE_ITEMS = new List<tablecell> ();
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return TABLE_ITEMS.Count;
}
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier); //if there are no cells to reuse , cerate a new one if (cell == null) cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier); cell.TextLabel.Text = TABLE_ITEMS [indexPath.Row].TITLE; cell.TextLabel.Font = UIFont.SystemFontOfSize(15f); cell.TextLabel.TextColor = new UIColor (0.453f, 0.453f, 0.453f, 1); cell.ImageView.Image = UIImage.FromBundle (TABLE_ITEMS[indexPath.Row].IMAGE); //cell front Image cell.Accessory = UITableViewCellAccessory.DisclosureIndicator; return cell;
}
public override void RowSelected (UITableView tableView, Foundation.NSIndexPath indexPath)
{
//把值拋到OptionViewController的Cell_RowSelected的事件
if (Cell_RowSelected != null) Cell_RowSelected(this,new TableRowSelectedEventArgs(){ indexPath = indexPath});
tableView.DeselectRow (indexPath, true); //cancel select row style
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40```csharp
//製作TableSource的Class
public class TableSource : UITableViewSource
{
string cellIdentifier = "OptionsTableCell";
public List<tablecell> TABLE_ITEMS{ get; set;} //每列資料 public event EventHandler<tablerowselectedeventargs> Cell_RowSelected;//給外層cell click事件
public TableSource ()
{
TABLE_ITEMS = new List<tablecell> ();
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return TABLE_ITEMS.Count;
}
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
//if there are no cells to reuse , cerate a new one
if (cell == null) cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = TABLE_ITEMS [indexPath.Row].TITLE;
cell.TextLabel.Font = UIFont.SystemFontOfSize(15f);
cell.TextLabel.TextColor = new UIColor (0.453f, 0.453f, 0.453f, 1);
cell.ImageView.Image = UIImage.FromBundle (TABLE_ITEMS[indexPath.Row].IMAGE); //cell front Image
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return cell;
}
public override void RowSelected (UITableView tableView, Foundation.NSIndexPath indexPath)
{
//把值拋到OptionViewController的Cell_RowSelected的事件
if (Cell_RowSelected != null) Cell_RowSelected(this,new TableRowSelectedEventArgs(){ indexPath = indexPath});
tableView.DeselectRow (indexPath, true); //cancel select row style
}
}1
2//Table Cell Classpublic class TableCell{ public string TITLE { get; set; } //標題 public string IMAGE { get; set;} //圖片 public TableCell () { }
public TableCell (string title , string image) { TITLE = title; IMAGE = image; }}然後在ViewController這麼做 ```csharp
public OptionViewController (IntPtr handle) : base (handle){ _tableview = SetTable (); this.Add (_tableview); //把Table加進去View裡面}////// Set Table Data/// ///The table. private UITableView SetTable(){ UITableView tv = new UITableView (View.Bounds); TableSource _source = new TableSource (); Listcelllist = new List (); //將options的menu選項加進table cell裡面 foreach (Options item in Enum.GetValues(typeof(Options))) { string[] descript = Tools.GetEnumDescription (item).Split(new String[]{“;”}, StringSplitOptions.RemoveEmptyEntries); celllist.Add(new TableCell (descript[0], descript[1]));//標題;圖片 } _source.TABLE_ITEMS = celllist; tv.Source = _source; //cell click觸發的事件 _source.Cell_RowSelected += (object sender, TableRowSelectedEventArgs e) => { switch ((Options)e.indexPath.Row) { case Options.BuildAge:NavigationController.PushViewController(Config.GetStoryboardViewController("Options_BuildAgeViewController"),true); break; }
}; return tv;}
```