Visual Studio で Xamarin 頑張る(Androidのみ)

Android でのすんげーちょっとした開発案件があり、Java でやってもいいのだけれど、周りが C# 使いなので Xamarin 無料になったということで調べた。

blogs.msdn.microsoft.com

ここで紹介されてたこのハンズオンした。良き資料。田淵さんすごい。

GitHub - ytabuchi/XamarinHOL: Xamarin ハンズオン用のレポジトリ&ドキュメントです。


なんか色々書いたけどやっぱり不適切だと思ったので削除。

それはそれとして、ハンズオンしてみたところ、Android開発だけならAndroid本見れば良さそうな感触を得たので以下を買った。初版は途中までJavaでやった。

4章まで

4章までのところは読み物。動かしてみる。デフォではlayout読み込み部分コメントアウトされてるので注意。
端末がへっぽこなので、デバッグは実機で。
f:id:yossk:20170331222226p:plain

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);
}
5.1
using System.IO;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Util;

namespace MyApplication
{
    [Activity(Label = "MyApplication", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            string version = "";
            try
            {
                using (var sr = new StreamReader("/proc/version"))
                {
                    version = sr.ReadLine();
                }
            }
            catch (FileNotFoundException ex)
            {
                Log.Error("tag", ex.ToString());
            }
            catch (IOException ex)
            {
                Log.Error("tag", ex.ToString());
            }

            var text = FindViewById<TextView>(Resource.Id.textView);
            text.Text = $"カーネルのバージョン:\n{version}";
        }
    }
}

f:id:yossk:20170331222449p:plain

5.2
using Android.App;
using Android.OS;
using Android.Widget;

namespace OperatingTIme
{
    [Activity(Label = "OperatingTIme", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView (Resource.Layout.Main);

            var ut = SystemClock.UptimeMillis();

            var tv2 = FindViewById<TextView>(Resource.Id.textView2);
            var tv3 = FindViewById<TextView>(Resource.Id.textView3);
            var tv4 = FindViewById<TextView>(Resource.Id.textView4);
            var tv5 = FindViewById<TextView>(Resource.Id.textView5);

            tv2.Text = $"(ミリ秒単位) {ut}";
            tv3.Text = $"(秒単位) {ut / 1000}";
            tv4.Text = $"(分単位) {ut / 1000 / 60}";
            tv5.Text = $"(時間単位) {ut / 1000 / 60 / 60} : {ut / 1000 / 60 % 60}";
        }
    }
}

f:id:yossk:20170331223111p:plain

まとめ

C# の記述で Android 開発できるのやばい。Visual Studio のエディタのパワーも相まって快適過ぎる。