첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

[TestProject 프로젝트]

▶ IDialer.cs

namespace TestProject
{
    /// <summary>
    /// 다이얼러 인터페이스
    /// </summary>
    public interface IDialer
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 전화 걸기 - Dial(phoneNumber)

        /// <summary>
        /// 전화 걸기
        /// </summary>
        /// <param name="phoneNumber">전화 번호</param>
        /// <returns>처리 결과</returns>
        bool Dial(string phoneNumber);

        #endregion
    }
}

 

728x90

 

▶ PhoneHelper.cs

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
    }
}

 

300x250

 

▶ MainPage.xaml

<?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

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

<?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>

 

▶ MainApplication.xaml.cs

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

 

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

[TestProject.Android 프로젝트]

▶ Dialer.cs

using System.Linq;

using Xamarin.Forms;

using Android.Content;
using Android.Content.PM;
using Android.Telephony;

using TestProject.Droid;

 

[assembly: Dependency(typeof(Dialer))]

▶ MainActivity.cs

using Android.App;
using Android.Content.PM;
using Android.OS;

namespace TestProject.Droid
{
    /// <summary>
    /// 메인 액티비티
    /// </summary>
    [Activity(Label = "TestProject", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Internal

        #region 인스턴스 - Instance

        /// <summary>
        /// 인스턴스
        /// </summary>
        internal static MainActivity Instance { get; private set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Protected

        #region 생성시 처리하기 - OnCreate(bundle)

        /// <summary>
        /// 생성시 처리하기
        /// </summary>
        /// <param name="bundle">번들</param>
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            Instance = this;

            global::Xamarin.Forms.Forms.Init(this, bundle);

            LoadApplication(new MainApplication());
        }

        #endregion
    }
}

 

[TestProject.iOS 프로젝트]

▶ Dialer.cs

using Xamarin.Forms;

using Foundation;
using UIKit;

using TestProject.iOS;

 

[assembly: Dependency(typeof(Dialer))]

▶ MainApplicationDelegate.cs

using Foundation;
using UIKit;

namespace TestProject.iOS
{
    /// <summary>
    /// 메인 애플리케이션 대리자
    /// </summary>
    [Register("MainApplicationDelegate")]
    public partial class MainApplicationDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 런칭 완료시 처리하기 - FinishedLaunching(application, optionDictionary)

        /// <summary>
        /// 런칭 완료시 처리하기
        /// </summary>
        /// <param name="application">애플리케이션</param>
        /// <param name="optionDictionary">옵션 딕셔너리</param>
        /// <returns>처리 결과</returns>
        public override bool FinishedLaunching(UIApplication application, NSDictionary optionDictionary)
        {
            global::Xamarin.Forms.Forms.Init();

            LoadApplication(new MainApplication());

            return base.FinishedLaunching(application, optionDictionary);
        }

        #endregion
    }
}

 

▶ Program.cs

using UIKit;

namespace TestProject.iOS
{
    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main(argumentArray)

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        /// <param name="argumentArray">인자 배열</param>
        private static void Main(string[] argumentArray)
        {
            UIApplication.Main(argumentArray, null, "MainApplicationDelegate");
        }

        #endregion
    }
}

 

[TestProject.UWP 프로젝트]

▶ Dialer.cs

using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Calls;
using Windows.UI.Popups;

using Xamarin.Forms;

using TestProject.UWP;

 

[assembly: Dependency(typeof(Dialer))]

▶ MainPage.xaml

<forms:WindowsPage x:Class="TestProject.UWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:forms="using:Xamarin.Forms.Platform.UWP"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
</forms:WindowsPage>

 

▶ MainPage.xaml.cs

namespace TestProject.UWP
{
    /// <summary>
    /// 메인 페이지
    /// </summary>
    public sealed partial class MainPage
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainPage()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainPage()
        {
            this.InitializeComponent();

            LoadApplication(new TestProject.MainApplication());
        }

        #endregion
    }
}

 

▶ App.xaml

<Application x:Class="TestProject.UWP.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Light">
</Application>

 

▶ App.xaml.cs

using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace TestProject.UWP
{
    /// <summary>
    /// 앱
    /// </summary>
    sealed partial class App : Application
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - App()

        /// <summary>
        /// 생성자
        /// </summary>
        public App()
        {
            this.InitializeComponent();

            this.Suspending += Application_Suspending;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 실행시 처리하기 - OnLaunched(e)

        /// <summary>
        /// 실행시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;

            if(rootFrame == null)
            {
                rootFrame = new Frame();

                rootFrame.NavigationFailed += rootFrame_NavigationFailed;

                Xamarin.Forms.Forms.Init(e);

                Window.Current.Content = rootFrame;
            }

            if(rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
            }

            Window.Current.Activate();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 애플리케이션 일시 중지시 처리하기 - Application_Suspending(sender, e)

        /// <summary>
        /// 애플리케이션 일시 중지시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Application_Suspending(object sender, SuspendingEventArgs e)
        {
            SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();

            deferral.Complete();
        }

        #endregion
        #region 루트 프레임 네비게이션 실패시 처리하기 - rootFrame_NavigationFailed(sender, e)

        /// <summary>
        /// 루트 프레임 네비게이션 실패시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 인자</param>
        /// <param name="e">이벤트 인자</param>
        private void rootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception(string.Format("페이지 로드시 실패하였습니다 : {0}", e.SourcePageType.FullName));
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)

'C# > Xamarin' 카테고리의 다른 글

[C#/XAMARIN] 전화 걸기  (0) 2018.05.19
Posted by icodebroker

댓글을 달아 주세요