[C#/XAMARIN] 전화 걸기
■ 전화를 거는 방법을 보여준다. [TestProject 프로젝트] ▶ IDialer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
namespace TestProject { /// <summary> /// 다이얼러 인터페이스 /// </summary> public interface IDialer { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 전화 걸기 - Dial(phoneNumber) /// <summary> /// 전화 걸기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>처리 결과</returns> bool Dial(string phoneNumber); #endregion } } |
▶ PhoneHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using System.Text; namespace TestProject { /// <summary> /// 전화 헬퍼 /// </summary> public static class PhoneHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 숫자 배열 /// </summary> private static readonly string[] _digitArray = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 전화 번호 구하기 - GetPhoneNumber(source) /// <summary> /// 전화 번호 구하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>전화 번호</returns> public static string GetPhoneNumber(string source) { if(string.IsNullOrWhiteSpace(source)) { return null; } source = source.ToUpperInvariant(); StringBuilder stringBuilder = new StringBuilder(); foreach(char character in source) { if (" -0123456789".Contains(character)) { stringBuilder.Append(character); } else { int? result = GetNumber(character); if(result != null) { stringBuilder.Append(result); } else { return null; } } } return stringBuilder.ToString(); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 포함 여부 구하기 - Contains(keyString, character) /// <summary> /// 포함 여부 구하기 /// </summary> /// <param name="keyString">키 문자열</param> /// <param name="character">문자</param> /// <returns>포함 여부</returns> private static bool Contains(this string keyString, char character) { return keyString.IndexOf(character) >= 0; } #endregion #region 번호 구하기 - GetNumber(character) /// <summary> /// 번호 구하기 /// </summary> /// <param name="character">문자</param> /// <returns>번호</returns> private static int? GetNumber(char character) { for(int i = 0; i < _digitArray.Length; i++) { if(_digitArray[i].Contains(character)) { return 2 + i; } } return null; } #endregion } } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestProject.MainPage"> <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"> <On Platform="iOS" Value="20 40 20 20" /> <On Platform="Android,UWP" Value="20" /> </OnPlatform> </ContentPage.Padding> <StackLayout> <Label Text="전화 번호 텍스트를 입력해 주시기 바랍니다 :" /> <Entry x:Name="phoneNumberText" Text="1-855-XAMARIN" /> <Button x:Name="translateButon" Text="번역하기" Clicked="translateButon_Clicked" /> <Button x:Name="callButton" Text="호출하기" IsEnabled="false" Clicked="callButton_Clicked" /> </StackLayout> </ContentPage> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using Xamarin.Forms; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 번역 번호 /// </summary> private string translateNumber; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 번역하기 버튼 클릭시 처리하기 - translateButon_Clicked(sender, e) /// <summary> /// 번역하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void translateButon_Clicked(object sender, EventArgs e) { this.translateNumber = PhoneHelper.GetPhoneNumber(this.phoneNumberText.Text); if(!string.IsNullOrWhiteSpace(this.translateNumber)) { this.callButton.IsEnabled = true; this.callButton.Text = translateNumber + " 전화걸기"; } else { this.callButton.IsEnabled = false; this.callButton.Text = "전화걸기"; } } #endregion #region 전화걸기 버튼 클릭시 처리하기 - callButton_Clicked(sender, e) /// <summary> /// 전화걸기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void callButton_Clicked(object sender, EventArgs e) { bool result = await this.DisplayAlert ( "전화걸기", string.Format("{0} 번호로 전화를 걸으시겠습니까?", this.translateNumber), "예", "아니오" ); if(result) { IDialer dialer = DependencyService.Get<IDialer>(); if(dialer != null) { dialer.Dial(this.translateNumber); } } } #endregion } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestProject.MainApplication"> </Application> |