LoginSignup
2
5

More than 5 years have passed since last update.

Xamarin.Formsのチュートリアル

Last updated at Posted at 2016-05-02

XamarinでiOS、Androidで共通のUIコードを書こうと思ったら、Xamarin.Formsを使う必要があります。

VisualStudio2015で、「Blank App (Android)」や「Blank App (iPhone)」などのテンプレートを使うと、UIを各OS用に独自にプログラミングしなくてはなりません。

共通のUIが書けるXamarin.Formsを使うには、「Blank App (Xamarin.Forms Portable)」というテンプレートを使います。(Xamarin.Forms Sharedとの違いはまだちょっと良く分かりませんが、今後分かったらメモ書きます)

BlankApp_Xamarin.Forms_Portable.png

1.Xamarin.Formsの最初の1歩

こちらのページに、Xamarin.Formsの入門用チュートリアルがあります。
https://developer.xamarin.com/guides/xamarin-forms/getting-started/hello-xamarin-forms/quickstart/

Xamarin.Formsの取っ掛かりとしてこちらのページに書かれている通りにプログラムを書いてみたのですが、一部そのままでは一部そのままではビルドできない箇所があります。

どこかというと、WindowsPhoneのプログラム。上記のホームページ上では下図のようになっているのですが、これはVisualStudio2015で作られるWindowsPhone 8.1ではビルドできません。

PhoneDialer.cs
// Windows Phone 8.1ではビルドエラーとなるコード
//(Xamarin.FormsのQuickStartのコードです)

using Microsoft.Phone.Tasks;
using Phoneword.WinPhone;
using Xamarin.Forms;

[assembly: Dependency(typeof(PhoneDialer))]

namespace Phoneword.WinPhone
{
    public class PhoneDialer : IDialer
    {
        public bool Dial(string number)
        {
            PhoneCallTask phoneCallTask = new PhoneCallTask
            {
                PhoneNumber = number, DisplayName = "Phoneword"
            };

            phoneCallTask.Show();
            return true;
        }
    }
}

WindowsPhone 8.1からは、Microsoft.Phone.Tasks名前空間が無くなっているそうで、それが原因でこのコードはビルドエラーとなってしまうそうです。
(参照:http://stackoverflow.com/questions/24997435/microsoft-phone-tasks-namespace-is-not-available)

で、どうするかというと、こんな感じに書いてみました。

PhoneDialer.cs
// Windows Phone 8.1でもビルドできました。

using Windows.ApplicationModel.Calls;
using Phoneword.WinPhone;
using Xamarin.Forms;

[assembly: Dependency(typeof(PhoneDialer))]

namespace Phoneword.WinPhone
{
    public class PhoneDialer : IDialer
    {
        public bool Dial(string number)
        {
            PhoneCallManager.ShowPhoneCallUI(number, "Phoneword");

            return true;
        }
    }
}

Windows.ApplicationModel.Calls名前空間のPhoneCallManagerクラスで、同様のことができるみたいです。
残念ながらWindows Phoneは持っていないので動作確認はしていないのですが...

2
5
0

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
2
5