一聚教程网:一个值得你收藏的教程网站

热门教程

Windows Phone7简单的计算器程序代码

时间:2022-06-26 00:18:48 编辑:袖梨 来源:一聚教程网

近来闲来无事,便下了个WP SDK7.1,感觉和WPF和Metro开发很相似,这不 ,就写了一个简单的计算器:

 

功能很简单,通过应用程序中的按钮实现加减乘除,暂不支持直接输入运算符

 代码如下 复制代码

 using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  using Microsoft.Phone.Controls;
 
  namespace PhoneApp9
  {
      public partial class MainPage : PhoneApplicationPage
      {
          // 构造函数
          public MainPage()
          {
              InitializeComponent();
          }
 
          public double Num1;
          yunSuanFu yunsuanfu = 0;
          bool isinput = false;
          double numresult;
          public enum yunSuanFu:int
          {
              jia=1,
              jian=2,
              cheng=3,
              chu=4
          }
          ///


          /// 计算数据
          ///

          /// 参数1
          /// 参数2
          /// 运算类型
          ///
          private double jisuan(double num1,double num2, yunSuanFu yunsuanfu)
          {
              double result;
              switch (yunsuanfu)
              {
                  case yunSuanFu.jia:
                      {
                          result = num1 + num2;
                          break;
                      }
                  case yunSuanFu.jian:
                      {
                          result = num1 - num2;
                          break;
                      }
                  case yunSuanFu.cheng:
                      {
                          result = num1 * num2;
                          break;
                      }
                  case yunSuanFu.chu:
                      {
                          result = num1 / num2;
                          break;
                      }
                  default:
                      {
                          result = 0;
                          break;
                      }
              }
              return result;
          }
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             
 
              string text = ((Button)sender).Content.ToString();
              int number;
              if (int.TryParse(text,out number) || text == ".")
              {
                  if (isinput == false)
              {
                  if (yunsuanfu != 0)
                  {
                      isinput = true;
                      showToText("");
                  }
              }
                  showcntent.Text += text;
              }
              else
              {
                  yunsuanfu = getYunsuanfu(text);
                  double num1 = Num1;
                  double num2=Convert.ToDouble(showcntent.Text);
                  numresult = jisuan(num1, num2, yunsuanfu);
                  Num1 = numresult;
                  showToText(numresult.ToString());
                  isinput = false;
              }
                 
 
          }
          private void showToText(string text)
          {
            
              showcntent.Text = text;
          }
          ///
          /// 获取运算符
          ///

          ///
          ///
          private yunSuanFu getYunsuanfu(string text)
          {
              yunSuanFu yunsuanfu=0;
              switch (text)
              {
                  case "+":
                      {
                          yunsuanfu = yunSuanFu.jia;
                          break;
                      }
                  case "-":
                      {
                          yunsuanfu = yunSuanFu.jian;
                          break;
 
                      }
                  case "*":
                      {
                          yunsuanfu = yunSuanFu.cheng;
                          break;
                      }
                  case "/":
                      {
                          yunsuanfu = yunSuanFu.chu;
                          break;
                      }
              }
              return yunsuanfu;
  
          }
          private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
          {
            
              showcntent.Text = "";
          }
 
          private void Button_Click_1(object sender, RoutedEventArgs e)
          {
              double num2 = Convert.ToDouble(showcntent.Text);
              numresult = jisuan(Num1, num2, yunsuanfu);
              showToText(numresult.ToString());
          }
      }
  }

View Code 
 

 代码如下 复制代码
using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  using Microsoft.Phone.Controls;
 
  namespace PhoneApp9
  {
      public partial class MainPage : PhoneApplicationPage
      {
          // 构造函数
          public MainPage()
          {
              InitializeComponent();
          }
 
          public double Num1;
          yunSuanFu yunsuanfu = 0;
          bool isinput = false;
          double numresult;
          public enum yunSuanFu:int
          {
              jia=1,
              jian=2,
              cheng=3,
              chu=4
          }
          ///
          /// 计算数据
          ///

          /// 参数1
          /// 参数2
          /// 运算类型
          ///
          private double jisuan(double num1,double num2, yunSuanFu yunsuanfu)
          {
              double result;
              switch (yunsuanfu)
              {
                  case yunSuanFu.jia:
                      {
                          result = num1 + num2;
                          break;
                      }
                  case yunSuanFu.jian:
                      {
                          result = num1 - num2;
                          break;
                      }
                  case yunSuanFu.cheng:
                      {
                          result = num1 * num2;
                          break;
                      }
                  case yunSuanFu.chu:
                      {
                          result = num1 / num2;
                          break;
                      }
                  default:
                      {
                          result = 0;
                          break;
                      }
              }
              return result;
          }
          private void Button_Click(object sender, RoutedEventArgs e)
          {
             
 
              string text = ((Button)sender).Content.ToString();
              int number;
              if (int.TryParse(text,out number) || text == ".")
              {
                  if (isinput == false)
              {
                  if (yunsuanfu != 0)
                  {
                      isinput = true;
                      showToText("");
                  }
              }
                  showcntent.Text += text;
              }
              else
              {
                  yunsuanfu = getYunsuanfu(text);
                  double num1 = Num1;
                  double num2=Convert.ToDouble(showcntent.Text);
                  numresult = jisuan(num1, num2, yunsuanfu);
                  Num1 = numresult;
                  showToText(numresult.ToString());
                  isinput = false;
              }
                 
 
          }
          private void showToText(string text)
          {
            
              showcntent.Text = text;
          }
          ///
          /// 获取运算符
          ///

          ///
          ///
          private yunSuanFu getYunsuanfu(string text)
          {
              yunSuanFu yunsuanfu=0;
              switch (text)
              {
                  case "+":
                      {
                          yunsuanfu = yunSuanFu.jia;
                          break;
                      }
                  case "-":
                      {
                          yunsuanfu = yunSuanFu.jian;
                          break;
 
                      }
                  case "*":
                      {
                          yunsuanfu = yunSuanFu.cheng;
                          break;
                      }
                  case "/":
                      {
                          yunsuanfu = yunSuanFu.chu;
                          break;
                      }
              }
              return yunsuanfu;
  
          }
          private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
          {
            
              showcntent.Text = "";
          }
 
          private void Button_Click_1(object sender, RoutedEventArgs e)
          {
              double num2 = Convert.ToDouble(showcntent.Text);
              numresult = jisuan(Num1, num2, yunsuanfu);
              showToText(numresult.ToString());
          }
      }
  }

xaml:
 

 代码如下 复制代码
       x:Class="PhoneApp9.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
     FontFamily="{StaticResource PhoneFontFamilyNormal}"
     FontSize="{StaticResource PhoneFontSizeNormal}"
     Foreground="{StaticResource PhoneForegroundBrush}"
     SupportedOrientations="Portrait" Orientation="Portrait"
     shell:SystemTray.IsVisible="True"
     Loaded="PhoneApplicationPage_Loaded">
 
    
    
        
            
            
        

 
        
        
            
            
        

 
        
        
            
                
                
            

            
                
                    
                    
                

                 <TextBox Name="showcntent">
                
                    
                         
                        
                        
                    

                    
                        
                        
                        
                    

                    
                        
                        
                        
                    

                    
                        
                        
                        
                    

                    
                        
                        
                        
                    

                

            

            
                
            

        

    

 
    
    
 
 

热门栏目