LoginSignup
8

More than 5 years have passed since last update.

Xamarinで遊ぶ(1) 起動するまでのメモ書き

Last updated at Posted at 2014-08-25

次→


最近 XamarinStudio で遊んでいます。チュートリアルにあった Taskyというサンプルコードがシンプルでいろいろ弄れるのですが、これと同じものを1から作っていこうと思います。

空のソリューションを作ってAppDelegateを弄る

iOSの「空のプロジェクト」を作ると、Main.csとAppDelegate.csだけのプロジェクトが生成されます。これを弄って、まずは「青い画面」が出るまで書き換えました。

Main.csは、objcのmain.m とほぼ同じです。

Main.cs
using System;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace MyTasky
{
    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, "AppDelegate");
        }
    }
}

AppDelegate.csも、objcで馴染みのある書き方です。ただC#特有の書き方が出てきます。

AppDelegate.cs
using System;
//using System.Collections.Generic;
//using System.Linq;

using MonoTouch.Foundation; // NSxxxはこっちに入っている?
using MonoTouch.UIKit;      // UIxxxはこっちに入っている

// https://xamarin.com/getting-started/ios にあるサンプル、Taskyを自力で作っていく
// 
// XamarinStudio用のgitignoreの作り方
// $git ignore xamarinstudio > .gitignore

namespace MyTasky
{
    // ↓ここを削ると起動しない。おそらく"objc側でのクラス登録"だと思うけど、[] の意味が分からない
    [MonoTouch.Foundation.Register ("AppDelegate")]  

    // ここのクラス名が、↑のRegisterとずれていても、objc的には↑のクラス名として登録されてるみたい。
    public partial class MyAppDelegate : UIApplicationDelegate 
    // "partial" 部分型宣言 1つの型を複数のファイルで宣言できる。
    // http://msdn.microsoft.com/ja-jp/library/wa80x488.aspx
    {
        UIWindow window;

        // 親クラスのメソッドをオーバーライドする時には、overrideが必要。無いとwarning & 真っ黒画面
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);

//          UIViewController vc = new UIViewController ();
            var vc = new UIViewController (); // 型推論でvarでいける
            vc.View.BackgroundColor = UIColor.Blue;

            UINavigationController navVC = new UINavigationController ();
            navVC.PushViewController (vc, false);

            // If you have defined a root view controller, set it here:
            window.RootViewController = navVC;

            // make the window visible
            this.window.MakeKeyAndVisible ();

            return true;
        }
    }
}


MonoTouch.* には何が入っているのか

using MonoTouch.xxx というのがあり、ここにUIKitやobjc関連のモノが入っているようです。
APIリファレンスを見ると、PassKitやらSpriteKitやら、見慣れたものがあります。
Mono_Documentation_Browser.png
MonoTouch.UIKit には、UIxxxやNSText〜なモノが入っていますが、"IUIxxx"なものも入っています。delegate等が入っているので、objcのプロトコルに対応する、C#向けのインタフェース定義なのでしょう。
Mono_Documentation_Browser.png

クラス宣言前の[Register("AppDelegate"]が、objcレベルでのクラス登録をしている

[Register("AppDelegate")] として、C#のクラス名は"MyAppDelegate"と別のものを指定してみましたが、これでもちゃんと起動しました。つまり「objcのAppDelegateクラスはC#のMyAppDelegateである」という対応を、[Register("AppDelegate")]でやっているみたいです。
ただここで使ってる [...] の書き方、なんて言うのでしょうね。シンタックスシュガーだとは思うのですが。

objcのYES/NOは C#のtrue/false

YES/NO方式、個人的には好きなのだけど、言語的にはニッチなので悲しいです。

型推論?してくれる

C#だと、変数の型をvarで省略する事が出来ます。ただ自分は慣れてないのでしばらく型指定で書いていきそうです。

ここまでのソース

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8