Visual Studio で Xamarin 頑張る(Androidのみ)
Android でのすんげーちょっとした開発案件があり、Java でやってもいいのだけれど、周りが C# 使いなので Xamarin 無料になったということで調べた。
ここで紹介されてたこのハンズオンした。良き資料。田淵さんすごい。
GitHub - ytabuchi/XamarinHOL: Xamarin ハンズオン用のレポジトリ&ドキュメントです。
なんか色々書いたけどやっぱり不適切だと思ったので削除。
それはそれとして、ハンズオンしてみたところ、Android開発だけならAndroid本見れば良さそうな感触を得たので以下を買った。初版は途中までJavaでやった。
Androidアプリ開発パーフェクトマスター[最新版] (Perfect Master)
- 作者: 金城俊哉
- 出版社/メーカー: 秀和システム
- 発売日: 2016/11/29
- メディア: 単行本
- この商品を含むブログを見る
4章まで
4章までのところは読み物。動かしてみる。デフォではlayout読み込み部分コメントアウトされてるので注意。
端末がへっぽこなので、デバッグは実機で。
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}"; } } }
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}"; } } }
まとめ
C# の記述で Android 開発できるのやばい。Visual Studio のエディタのパワーも相まって快適過ぎる。