Xamarin.Android JavaからC#への書き換え
Xamarin.AndroidにおいてJavaはC#でラッピングされています。ですが、コードの命名はJavaはキャメルでC#がパスカルでそれを筆頭に書き方の作法の違いがあります。
そこで、それの雰囲気を伝えるために下記を書きました。
Xamarin.Androidのプロジェクトを作る
新規プロジェクトを作ってください。
ボタンを増やす
ボタンを配置しましょう。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="25px" android:minHeight="25px" android:id="@+id/button1" /> </RelativeLayout>[f:id:Jirobe_Katori:20190322062544g:plain]
ボタン押下にイベントを紐づける
ボタンにイベントハンドラを足しておきましょう。
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); Button button1 = FindViewById<Button>(Resource.Id.button1); button1.Click += (s, e) => { }; } }
javaを検索する
今回はカメラの起動をしてみましょう。Javaの記事を探します。
今回はこちらの内容がよさそうです。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, RESULT_CAMERA);
[Android] Camera で撮影、Intentで簡単にやりましょ (https://akira-watson.com/android/camera-intent.html)
JavaをC#のソースに張り付け、そして考えずに感じてください。
int RESULT_CAMERA = 1001; Button button1 = FindViewById<Button>(Resource.Id.button1); button1.Click += (s, e) => { Intent intent = new Intent(MediaStore.ActionImageCapture); StartActivityForResult(intent, RESULT_CAMERA); };
アプリを実行
Android上でアプリを実行してみましょう。カメラが起動するはずです。