XamarinでiPhoneアプリをビルドしてみる
とりあえずXamarinでiOSアプリを作ってみます。
HelloWorldでは面白みがないので、時計のアプリでも作ってみます。
1. Visual Studioで新規ソリューションを作成します。
テンプレートは「iPhoneのSingle View App(iPhone)」を選びます。
名前は「SimpleClock」にしておきます。
2.「ツール-オプション」メニューで、Xamarinの「iOS Setting」のページを開きます。
3.リモートビルド用のMacに接続します。接続する際、MacのログインID、パスワードを入力します。
4.Main.storyboardを開きます。ViewはiPhone6、iOS9.2を選びます。
5.左のツールボックスからLabelを選んで貼り付けます。フォントサイズを適当に大きくし、Name属性には「labelTime」と入力します。
6.ViewControl.csを開き、下記のとおり編集します。
using System;
using System.Timers;using UIKit;
namespace SimpleClock
{
public partial class ViewController : UIViewController
{
Timer timer;public ViewController(IntPtr handle) : base(handle)
{
}public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.// 画面の初期化
updateContorol();// 1秒ごとに画面を更新する
timer = new Timer(1000);
timer.Elapsed += ((source, e) =>
{
InvokeOnMainThread(() => {
updateContorol();
});
});
timer.AutoReset = true;
timer.Enabled = true;
}public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}private void updateContorol()
{
labelTime.Text = DateTime.Now.ToLongTimeString();
}
}
}