Windows Phone7简单的计算器程序代码
近来闲来无事,便下了个WP SDK7.1,感觉和WPF和Metro开发很相似,这不 ,就写了一个简单的计算器:
功能很简单,通过应用程序中的按钮实现加减乘除,暂不支持直接输入运算符
代码如下 | 复制代码 |
using System; |
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 } /// <summary> /// 计算数据 /// </summary> /// <param name="num1">参数1</param> /// <param name="num2">参数2</param> /// <param name="yunsuanfu">运算类型</param> /// <returns></returns> 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; } /// <summary> /// 获取运算符 /// </summary> /// <param name="text"></param> /// <returns></returns> 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:
代码如下 | 复制代码 |
<phone:PhoneApplicationPage 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"> <!--LayoutRoot 是包含所有页面内容的根网格--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="简易计算器" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="计算器" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBox Name="showcntent"></TextBox> <StackPanel Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Orientation="Horizontal"> <Button Height="70" Width="70" Click="Button_Click">1</Button> <Button Height="70" Width="70" Click="Button_Click">2</Button> <Button Height="70" Width="70" Click="Button_Click">3</Button> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Height="70" Width="70" Click="Button_Click">4</Button> <Button Height="70" Width="70" Click="Button_Click">5</Button> <Button Height="70" Width="70" Click="Button_Click">6</Button> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Height="70" Width="70" Click="Button_Click">7</Button> <Button Height="70" Width="70" Click="Button_Click">8</Button> <Button Height="70" Width="70" Click="Button_Click">9</Button> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Height="70" Width="70" Click="Button_Click">0</Button> <Button Height="70" Width="70" Click="Button_Click">.</Button> <Button Height="70" Width="70" Click="Button_Click">+</Button> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Height="70" Width="70" Click="Button_Click">-</Button> <Button Height="70" Width="70" Click="Button_Click">*</Button> <Button Height="70" Width="70" Click="Button_Click">/</Button> </StackPanel> </StackPanel> </Grid> <StackPanel Grid.Row="1"> <Button Click="Button_Click_1">计算</Button> </StackPanel> </Grid> </Grid> <!--演示 ApplicationBar 用法的示例代码--> <!--<phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/> <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="菜单项 1"/> <shell:ApplicationBarMenuItem Text="菜单项 2"/> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>--> </phone:PhoneApplicationPage> |