0%

[Xamarin] Xamarin的奇幻旅程

最近因為公司需要,開始學習如何用Xamarin開發 IOS,雖然主打用C#開發APP,但無論生命週期跟呼叫的function跟傳統的windows forms或ASP MVC都有相當大的差異性,有很大的一段GAP要跨過就是.

這邊紀錄一下我開發**IOS**時遇到的問題跟心得
  • AppDelegate,程式的進入點

    1
    2
    3
    4
    5
    6
    public 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
    2
    UIStoryboard.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
2
3
4
5
6
7
8
9
10
11

* IOS抓取對應圖片的方式
[![](//3.bp.blogspot.com/-yKVU6385RPk/VGRo5f3lZII/AAAAAAAAET0/Kz-_aQWVyLA/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%2B2014-11-13%2B%E4%B8%8B%E5%8D%884.15.59.png)](https://3.bp.blogspot.com/-yKVU6385RPk/VGRo5f3lZII/AAAAAAAAET0/Kz-_aQWVyLA/s1600/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%2B2014-11-13%2B%E4%B8%8B%E5%8D%884.15.59.png)```csharp
//FromBundle的方式IOS會自動對應不同的裝置抓取預備好給他的圖
//圖片檔名命名方式//[imageName]~iphone.png
//[imageName]~ipad.png
//[imageName]@2~ipad.png for retina顯示器
UIImage.FromBundle ("icon-options");
//FromFile不管任何解析度都是用這張來顯示,所以用前者的方式較佳
UIImage.FromFile ("icon-options.png");

  • TableViewController的製作方式
    ```csharp
    //製作TableSource的Class
    public class TableSource : UITableViewSource
    {
    string cellIdentifier = “OptionsTableCell”;
    public List TABLE_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 (); List celllist = 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) =&gt;    {       switch ((Options)e.indexPath.Row)       {          case Options.BuildAge:NavigationController.PushViewController(Config.GetStoryboardViewController("Options_BuildAgeViewController"),true);       break;       }
    

    }; return tv;}

```