Windows Phone using Visual Studio Silverlight for Windows Phone
Background on .NET and C#
The .NET framework is the
central concept behind Visual Studio 2010. It consists of two main elements the
Common Language Runtime (CLR) in which the application is executed and the set
of libraries called the .NET Framework Class Libraries which provides
functional support of the code needed.
The .NET programs are written in C++ /CLI and C# i.e. any language that
supports the .NET framework.
.Net is a collection of dynamically
linked libraries (DLLs), these DLLs are many times already installed on
Windows.
using System;//using statement obtains one or more resources using System.Collections.Generic;//This contains interfaces and classes that define generic collections, using System.Linq;//provides classes and interfaces that support queries that use Language-Integrated Query using System.Net; //provides a simple programming interface for many of the protocols used on networks using System.Windows;//Provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes. using System.Windows.Controls;//Represents the base class for user interface (UI) elements using System.Windows.Documents;//Contains types that support XML Paper Specification (XPS) document creation using System.Windows.Input;//Provides types to support the Windows Presentation Foundation (WPF) input system using System.Windows.Media;//Provides types that enable integration of media, including drawings, text etc using System.Windows.Media.Animation;//Provides types that support property animation functionality using System.Windows.Shapes;//Provides access to a library of shapes that can be used in(XAML) using Microsoft.Phone.Controls;//Provides controls for use in Window Phone Apps //namespace keyword used to declare scope that contains set of related objects namespace Lab3_Calculator_App { // by declaring partial class we can seperate class into multiple files // MainPage inherits from class PhoneApplicationPage, controls Windows Phone projects public partial class MainPage : PhoneApplicationPage { const int TRUE = 1; // TRUE in constant variable set to 1 const int FALSE = 0;// FALSE in constant variable set to 0 static int newvalue = TRUE; // static variable is associated with class itself rather than instance of class static float runningTotal = 0; //running total set to default value of 0 static char previousOp = '+'; //previous operator always set to plus // Constructor to initialize component // public keyword means the method is visible from outside the class public MainPage() { //Initialize component method contains code that initialize user //interface objects using property grid from form designer. InitializeComponent(); } // end of main page() method //start of method button0_Click //object sender refers to the object the invoked the event //RoutedEventArgs Gets or sets a reference to the object that raised the event private void button0_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "0"; // set text box string to 0 newvalue = FALSE; // now 0 is no longer first integer } else // else add 0 to current value in the text box { textBox1.Text += "0"; // 0 is added to current value in text box } // end of if else statement } // end of button0_Click method //start of method button1_Click //object sender refers to the object the invoked the event //RoutedEventArgs Gets or sets a reference to the object that raised the event private void button1_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "1"; // set text box string to 1 newvalue = FALSE; // now 1 is no longer first integer } else // else add 1 to current value in the text box { textBox1.Text += "1"; // 1 is added to current value in text box }// end of if else statement }// end of method button1_Click //start of method button2_Click private void button2_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "2"; // set text box string to 2 newvalue = FALSE;// now 2 is no longer first integer } else // else add 2 to current value in the text box { textBox1.Text += "2"; // 2 is added to current value in text box }// end of if else statement }// end of method button2_Click //start of method button3_Click private void button3_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "3"; // set text box string to 3 newvalue = FALSE; // now 3 is no longer first integer } else // else add 2 to current value in the text box { textBox1.Text += "3"; // 3 is added to current value in text box } // end of if else statement }// end of method button3_Click //start of method button4_Click private void button4_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "4"; // set text box string to 4 newvalue = FALSE; // now 4 is no longer first integer } else // else add 4 to current value in the text box { textBox1.Text += "4"; // 4 is added to current value in text box }// end of if else statement }// end of method button4_Click //start of method button5_Click private void button5_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) // if newvalue is first value to be pressed { textBox1.Text = "5"; // set text box string to 5 newvalue = FALSE;// now 5 is no longer first integer } else// else add 5 to current value in the text box { textBox1.Text += "5";// 5 is added to current value in text box }// end of if else statement }// end of method button5_Click //start of method button6_Click private void button6_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE)//if newvalue is first value to be pressed { textBox1.Text = "6";//set text box string to 6 newvalue = FALSE;// now 6 is no longer first integer } else// else add 6 to current value in the text box { textBox1.Text += "6";//6 is added to current value in text box }// end of if else statement }// end of method button6_Click private void button7_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) { textBox1.Text = "7"; newvalue = FALSE; } else { textBox1.Text += "7"; } } private void button8_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) { textBox1.Text = "8"; newvalue = FALSE; } else { textBox1.Text += "8"; } } private void button9_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE) { textBox1.Text = "9"; newvalue = FALSE; } else { textBox1.Text += "9"; } } //start of method buttopoint_Click private void buttopoint_Click(object sender, RoutedEventArgs e) { if (newvalue == TRUE)//if newvalue is first value to be pressed { textBox1.Text = ".";//set text box string to point newvalue = FALSE;// now point is no longer first integer } else// else add point to current value in the text box { textBox1.Text += ".";//point is added to current value in text box }// end of if else statement } // end of method buttopoint_Click //start of method plus_button_click //private method means it not visible from outside of the class private void plus_button_click(object sender, RoutedEventArgs e) { float rez1; // local variable to store result1 rez1 = System.Convert.ToSingle(textBox1.Text);// rez1 is given converted string contained within textbox //method ToSingle Converts the specified string representation of a number to //an equivalent single-precision floating-point number. //parameters passed to method ToSingle is text of textbox1 switch(previousOp) // switch statement to control selection of operators { case '+': runningTotal = runningTotal + rez1;//current result is added to running total break;//The break statement terminates the closest enclosing switch statement case '-': runningTotal = runningTotal - rez1;//current result is subtracted to running total break;//The break statement terminates the closest enclosing switch statement case '/': runningTotal = runningTotal / rez1;//current result is divide to running total break;//The break statement terminates the closest enclosing switch statement case '*': runningTotal = runningTotal * rez1;//current result is multiplied to running total break;//The break statement terminates the closest enclosing switch statement case '=': break;//The break statement terminates the closest enclosing switch statement } textBox1.Text = runningTotal.ToString(); // runningTotal is put on textbox but first is converted to the // the runningTotal from float to string newvalue = TRUE; // new value is now true previousOp = '+'; // previousOp is set to plus }// End of method plus_button_click //start of method subtract_button_click private void subtract_button_click(object sender, RoutedEventArgs e) { float rez1;// local variable to store result1 rez1 = System.Convert.ToSingle(textBox1.Text); // rez1 is given converted string contained within textbox //method ToSingle Converts the specified string representation of a number to an equivalent single-precision floating-point number. //parameters passed to method ToSingle is text of textbox1 switch(previousOp) // switch statement to control selection of operators { case '+': runningTotal = runningTotal + rez1; break;//The break statement terminates the closest enclosing switch statement case '-': runningTotal = runningTotal - rez1; break;//The break statement terminates the closest enclosing switch statement case '/': runningTotal = runningTotal / rez1; break;//The break statement terminates the closest enclosing switch statement case '*': runningTotal = runningTotal * rez1; break;//The break statement terminates the closest enclosing switch statement case '=': break;//The break statement terminates the closest enclosing switch statement } textBox1.Text = runningTotal.ToString();// runningTotal is put on textbox but first is converted to the // the runningTotal from float to string newvalue = TRUE;// new value is now true previousOp = '-';// previousOp is set to minus }//End of method subtract_button_click //start of method divide_button_click private void divide_button_click(object sender, RoutedEventArgs e) { float rez1;// local variable to store result1 rez1 = System.Convert.ToSingle(textBox1.Text);// rez1 is given converted string contained within textbox //method ToSingle Converts the specified string representation of a number to an equivalent single-precision floating-point number. //parameters passed to method ToSingle is text of textbox1 switch(previousOp) // switch statement to control selection of operators { case '+': runningTotal = runningTotal + rez1; break; case '-': runningTotal = runningTotal - rez1; break; case '/': runningTotal = runningTotal / rez1; break; case '*': runningTotal = runningTotal * rez1; break; case '=': break; } textBox1.Text = runningTotal.ToString();// runningTotal is put on textbox but first is converted to the // the runningTotal from float to string newvalue = TRUE; previousOp = '/';// previousOp is set to divide //End of method divide_button_click //start of method multiply_button_click private void multiply_button_click(object sender, RoutedEventArgs e) { float rez1;// local variable to store result1 rez1 = System.Convert.ToSingle(textBox1.Text);// rez1 is given converted string contained within textbox //method ToSingle Converts the specified string representation of a number to //an equivalent single-precision floating-point number. //parameters passed to method ToSingle is text of textbox1 switch(previousOp) // switch statement to control selection of operators { case '+': runningTotal = runningTotal + rez1; break;//The break statement terminates the closest enclosing switch statement case '-': runningTotal = runningTotal - rez1; break;//The break statement terminates the closest enclosing switch statement case '/': runningTotal = runningTotal / rez1; break;//The break statement terminates the closest enclosing switch statement case '*': runningTotal = runningTotal * rez1; break;//The break statement terminates the closest enclosing switch statement case '=': break;//The break statement terminates the closest enclosing switch statement } textBox1.Text = runningTotal.ToString();// runningTotal is put on textbox but first is converted to the // the runningTotal from float to string newvalue = TRUE; previousOp = '*';// previousOp is set to multiply }// end of method multiply_button_click private void equals_button_click(object sender, RoutedEventArgs e) { float rez1;// local variable to store result1 rez1 = System.Convert.ToSingle(textBox1.Text);// rez1 is given converted string contained within textbox //method ToSingle Converts the specified string representation //of a number to an equivalent single-precision floating-point number. //parameters passed to method ToSingle is text of textbox1 switch(previousOp) // switch statement to control selection of operators { case '+': runningTotal = runningTotal + rez1; break;//The break statement terminates the closest enclosing switch statement case '-': runningTotal = runningTotal - rez1; break;//The break statement terminates the closest enclosing switch statement case '/': runningTotal = runningTotal / rez1; break;//The break statement terminates the closest enclosing switch statement case '*': runningTotal = runningTotal * rez1; break;//The break statement terminates the closest enclosing switch statement case '=': break;//The break statement terminates the closest enclosing switch statement } textBox1.Text = runningTotal.ToString();// runningTotal is put on textbox but first is converted to the // the runningTotal from float to string newvalue = TRUE; runningTotal = 0; previousOp = '+'; } private void cancel_Click(object sender, RoutedEventArgs e) { textBox1.Text = ""; //text box is cleared when cancel is clicked runningTotal = 0; // running total is set 0 with screen cleared } private void Calculator_Loaded(object sender, RoutedEventArgs e) { } } // end of public partial class MainPage }// end of scope of namespace Lab3_Calculator_App
<phone:PhoneApplicationPage x:Class="Lab3_Calculator_App.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" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True" Name="Calculator" Loaded="Calculator_Loaded" d:DesignHeight="768" d:DesignWidth="480"> <phone:PhoneApplicationPage.BorderBrush> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFF7C7C" Offset="1" /> </LinearGradientBrush> </phone:PhoneApplicationPage.BorderBrush> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="Calculator" Style="{StaticResource PhoneTextNormalStyle}" TextAlignment="Center" Foreground="White" TextWrapping="NoWrap" OpacityMask="#FF77E8CC" FontFamily="Segoe WP SemiLight" FontWeight="Normal" FontSize="24" /> </StackPanel> <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,133,12,0"> <Button Content="1" Height="75" HorizontalAlignment="Left" Margin="12,79,0,0" Name="button1" VerticalAlignment="Top" Width="113" Foreground="WhiteSmoke" BorderBrush="#FFF8F8F8" Click="button1_Click" OpacityMask="#FFE10606" BorderThickness="4"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFCFF74" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="2" Height="75" HorizontalAlignment="Left" Margin="131,79,0,0" Name="button2" VerticalAlignment="Top" Width="111" Click="button2_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF77" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="3" Height="75" HorizontalAlignment="Left" Margin="242,78,0,0" Name="button3" VerticalAlignment="Top" Width="114" Click="button3_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFCFC27" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="4" Height="75" HorizontalAlignment="Left" Margin="12,176,0,0" Name="button4" VerticalAlignment="Top" Width="113" Click="button4_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF2F" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="5" Height="75" HorizontalAlignment="Left" Margin="131,176,0,0" Name="button5" VerticalAlignment="Top" Width="111" Click="button5_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF58" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="6" Height="75" HorizontalAlignment="Left" Margin="242,175,0,0" Name="button6" VerticalAlignment="Top" Width="114" Click="button6_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF08" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="9" Height="75" HorizontalAlignment="Left" Margin="242,267,0,0" Name="button7" VerticalAlignment="Top" Width="114" Click="button9_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF33" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="7" Height="75" HorizontalAlignment="Left" Margin="12,268,0,0" Name="button8" VerticalAlignment="Top" Width="113" Click="button7_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFE6FF00" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="8" Height="75" HorizontalAlignment="Left" Margin="131,268,0,0" Name="button9" VerticalAlignment="Top" Width="111" Click="button8_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFE54E" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="." Height="75" HorizontalAlignment="Left" Margin="131,369,0,0" Name="button10" VerticalAlignment="Top" Width="111" Click="buttopoint_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF27" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="C" Height="74" Margin="345,369,0,0" Name="button11" VerticalAlignment="Top" Click="cancel_Click" HorizontalAlignment="Left" Width="111"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF17" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="0" Height="75" HorizontalAlignment="Left" Margin="12,369,0,0" Name="button12" VerticalAlignment="Top" Width="116" Click="button0_Click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="Yellow" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="+" Height="75" HorizontalAlignment="Left" Margin="346,78,0,0" Name="button13" VerticalAlignment="Top" Width="110" Click="plus_button_click" BorderBrush="White" Foreground="White"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFEFEF00" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="-" Height="75" HorizontalAlignment="Left" Margin="346,175,0,0" Name="button14" VerticalAlignment="Top" Width="110" Click="subtract_button_click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF22" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="*" Height="75" HorizontalAlignment="Left" Margin="345,0,0,216" Name="button15" VerticalAlignment="Bottom" Width="111" Click="multiply_button_click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF12" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="/" Height="75" HorizontalAlignment="Left" Margin="242,368,0,0" Name="button16" VerticalAlignment="Top" Width="114" Click="divide_button_click"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFFFF2F" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Button Content="=" Height="78" HorizontalAlignment="Center" Margin="12,449,207,0" Name="button17" VerticalAlignment="Top" Width="237" Click="equals_button_click" BorderThickness="3" FontSize="28" FontWeight="Thin" FontStyle="Normal" FontStretch="SemiCondensed" Foreground="White" HorizontalContentAlignment="Center"> <Button.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFAFF00" Offset="1" /> </LinearGradientBrush> </Button.Background> </Button> <Grid.Background> <ImageBrush ImageSource="/Lab3_Calculator_App;component/Images/Bliss_Clean_by_mtspknwildcat.jpg" /> </Grid.Background> </Grid> <TextBox Height="149" HorizontalAlignment="Left" Margin="0,55,0,0" Name="textBox1" Text="0" VerticalAlignment="Top" Width="468" BorderBrush="#BFFFFFFF" TextAlignment="Right" FontSize="40" TextChanged="textBox1_TextChanged" Grid.RowSpan="2" OpacityMask="#FFFF1717" AllowDrop="False"> <TextBox.CaretBrush> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FFFF8383" Offset="1" /> </LinearGradientBrush> </TextBox.CaretBrush> <TextBox.Background> <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> <GradientStop Color="Black" Offset="0" /> <GradientStop Color="#FF96FFFF" Offset="1" /> </LinearGradientBrush> </TextBox.Background> </TextBox> </Grid> <!--Sample code showing usage of ApplicationBar--> <!--<phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/> <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="MenuItem 1"/> <shell:ApplicationBarMenuItem Text="MenuItem 2"/> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>--> </phone:PhoneApplicationPage>
You can download entire project in link below:
https://github.com/JunSoftware109/Mobile-Applications/tree/master/Windows%20Phone%20Apps/Lab3_Calculator_App
No comments:
Post a Comment