2017년 6월 9일 금요일

Visual Studio for mac으로 작성된 계산기 앱 소스 입니다.

자마린 스튜디오가 비주얼 스튜디오로 흡수되었습니다.
얼마전 정식 버전이 출시되었죠...
수업용 예제로 사용할 계산기 전체 소스입니다.
C# 코드 + XAML 태그로 구성되어 있습니다.
Xamarin Forms로 만들면 원소스를 아이폰, 안드로이드폰, 윈도우폰 등에서 사용할 수 있습니다.







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="Calculator.MainPage">

    <Grid Padding="5,0" RowSpacing="1"
        ColumnSpacing="1" BackgroundColor="Black">
        <Grid.RowDefinitions>
            <Grid.RowDefinition Height="2*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label FontAttributes="Bold" 
                FontSize="48" 
                BackgroundColor="Black" Text="0"
                TextColor="White" 
                HorizontalTextAlignment="End" 
                VerticalTextAlignment="Center"
            LineBreakMode="NoWrap" Grid.ColumnSpan="4" />
        <Button Text="7" Grid.Row="1" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="8" Grid.Row="1" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="9" Grid.Row="1" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />

        <Button Text="4" Grid.Row="2" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="5" Grid.Row="2" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="6" Grid.Row="2" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />

        <Button Text="1" Grid.Row="3" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="2" Grid.Row="3" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="3" Grid.Row="3" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />
        <Button Text="0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" />

        <Button Text="/" Grid.Row="1" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" />
        <Button Text="X" Grid.Row="2" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" />
        <Button Text="-" Grid.Row="3" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" />
        <Button Text="+" Grid.Row="4" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" />

        <Button Text="C" Grid.Row="5" Grid.Column="0"
                BackgroundColor="#808080" TextColor="White"
                FontSize="36" BorderRadius="0" />

        <Button Text="=" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" />
    </Grid>

</ContentPage>


(이벤트를 모두 연결한 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="Calculator.MainPage">

    <Grid Padding="5,0" RowSpacing="1"
        ColumnSpacing="1" BackgroundColor="Black">
        <Grid.RowDefinitions>
          <RowDefinition Height="2*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
       </Grid.RowDefinitions>

       <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
       </Grid.ColumnDefinitions>
        
        <Label x:Name="resultText" 
                FontAttributes="Bold" 
                FontSize="48" 
                BackgroundColor="Black" Text="0"
                TextColor="White" 
                HorizontalTextAlignment="End" 
                VerticalTextAlignment="Center"
            LineBreakMode="NoWrap" Grid.ColumnSpan="4" />
        <Button Text="7" Grid.Row="1" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" />
        <Button Text="8" Grid.Row="1" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" /> 
        <Button Text="9" Grid.Row="1" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" />

        <Button Text="4" Grid.Row="2" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
                Clicked="OnSelectNumber" />
        <Button Text="5" Grid.Row="2" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" />
        <Button Text="6" Grid.Row="2" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" />

        <Button Text="1" Grid.Row="3" Grid.Column="0"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0"
               Clicked="OnSelectNumber" /> 
        <Button Text="2" Grid.Row="3" Grid.Column="1"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" 
               Clicked="OnSelectNumber" /> 
        <Button Text="3" Grid.Row="3" Grid.Column="2"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" 
               Clicked="OnSelectNumber" /> 
        <Button Text="0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3"
               BackgroundColor="White" TextColor="Black"
               FontSize="36" BorderRadius="0" 
               Clicked="OnSelectNumber" /> 

        <Button Text="/" Grid.Row="1" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" 
                Clicked="OnSelectOperator" />
        <Button Text="X" Grid.Row="2" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" 
                Clicked="OnSelectOperator" />
        <Button Text="-" Grid.Row="3" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" 
                Clicked="OnSelectOperator" />
        <Button Text="+" Grid.Row="4" Grid.Column="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0" 
                Clicked="OnSelectOperator" />

        <Button Text="C" Grid.Row="5" Grid.Column="0"
                BackgroundColor="#808080" TextColor="White"
                FontSize="36" BorderRadius="0" 
                Clicked="OnClear" />

        <Button Text="=" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3"
                BackgroundColor="#FFA500" TextColor="White"
                FontSize="36" BorderRadius="0"
                Clicked="OnCalculate" />
    </Grid>

</ContentPage>

(로직이 모두 추가된 MainPage.xaml.cs파일)
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Calculator
{
    public partial class MainPage : ContentPage
    {
int currentState = 1;
string mathOperator;
double firstNumber, secondNumber;

        public MainPage()
        {
            InitializeComponent();
            //reset하는 코드추가 
            OnClear(this, null); 
        }
void OnSelectNumber(object sender, EventArgs e)
{
Button button = (Button)sender;
string pressed = button.Text;

if (this.resultText.Text == "0" || currentState < 0)
{
this.resultText.Text = "";
if (currentState < 0)
currentState *= -1;
}

this.resultText.Text += pressed;

double number;
if (double.TryParse(this.resultText.Text, out number))
{
this.resultText.Text = number.ToString("N0");
if (currentState == 1)
{
firstNumber = number;
}
else
{
secondNumber = number;
}
}
}

void OnSelectOperator(object sender, EventArgs e)
{
currentState = -2;
Button button = (Button)sender;
string pressed = button.Text;
mathOperator = pressed;
}

void OnClear(object sender, EventArgs e)
{
firstNumber = 0;
secondNumber = 0;
currentState = 1;
this.resultText.Text = "0";
}

void OnCalculate(object sender, EventArgs e)
{
if (currentState == 2)
{
var result = SimpleCalculator.Calculate(firstNumber, secondNumber, mathOperator);

this.resultText.Text = result.ToString("N0");
firstNumber = result;
currentState = -1;
}
}

    }
}

(SharedResources.cs파일)
using System;
using Xamarin.Forms; 

namespace Calculator
{
    public static class SharedResources
    {
        public static Color OpButtonBkColor
        {
            get { return Color.FromRgb(0xff, 0xa5, 0);  }
        }
    }
}



댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.

5월 14일 새벽에 chatGPT 4o가 발표되었습니다. 옵티마이즈, 옴니라는 의미인데 실시간 통역, 다자간 회의, 멀티모달 기능의 강화등이 보이네요.

  초격차로 OpenAI진영이 다시 앞서가는 모양을 보여주고 있습니다. 저도 새벽에 일어나자 마자 올라온 영상들과 글을 정리하고 있습니다. ㅎㅎ 영화 HER의 사진이 새벽에 많이 올라왔었는데 저도 안본 영화입니다. 주말에 한번 봐야 할 것 같습니다....