Recent Posts
- November 2014 (1)
- January 2014 (1)
- October 2013 (1)
- May 2013 (1)
- February 2013 (2)
- October 2012 (1)
- August 2012 (1)
- July 2012 (4)
- June 2012 (13)
Blog Stats
- 36,360 hits
Write your code smartly
Hello,
Today this post will give you an idea about , how you can get the current location of your Windows Phone device. This can be easily achieved by using `GeoCoordinateWatcher` class under the namespace using System.Device.Location;
Here is the code by which you can get the Longitude and Latitude values of the device location.
This code gets the current position of Windows Phone device in the form of Longitude and Latitude. Using these values, many different Location features can be implemented such as distance finding between two locations , mapping the location on Bing and Google maps and more.
For any further query you can contact on below addresses:
Email: jkhan@programmer.net
Skype: jibrankhan1990
In previous post I explained the simple process of getting started guide with Windows Phone development and how to configure it in your development PC. In this post I will show how to create a simple calculator with addition property ( you can alter by your self with its operator -, *, /). The new project with simple Windows phone page, drop 2 text boxes from toolbar on left and a button. Now click on a text box and on the right bottom you will see a property window, find the property “TEXT” and remove the word text box written in front of it. Do this for other as well. Now change the button “Content” property to “Add”.
If you want to create text boxes and button through code then use the following code sample and place it inside the second grid tag.
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”10,43,0,0″ Name=”textBox1″ Text=”” VerticalAlignment=”Top” Width=”460″ Grid.Row=”1″ />
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”10,125,0,0″ Name=”textBox2″ Text=”” VerticalAlignment=”Top” Width=”460″ Grid.Row=”1″ />
<Button Content=”Add” Grid.Row=”1″ Height=”72″ HorizontalAlignment=”Left” Margin=”152,209,0,0″ Name=”button1″ VerticalAlignment=”Top” Width=”160″ />
Here how now design view XAML code looks like
<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=”MY APPLICATION” Style=”{StaticResource PhoneTextNormalStyle}”/>
<TextBlock x:Name=”PageTitle” Text=”page name” Margin=”9,-7,0,0″ Style=”{StaticResource PhoneTextTitle1Style}”/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″></Grid>
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”10,43,0,0″ Name=”textBox1″ Text=”” VerticalAlignment=”Top” Width=”460″ Grid.Row=”1″ />
<TextBox Height=”72″ HorizontalAlignment=”Left” Margin=”10,125,0,0″ Name=”textBox2″ Text=”” VerticalAlignment=”Top” Width=”460″ Grid.Row=”1″ />
<Button Content=”Add” Grid.Row=”1″ Height=”72″ HorizontalAlignment=”Left” Margin=”152,209,0,0″ Name=”button1″ VerticalAlignment=”Top” Width=”160″ />
</Grid>
Now select the Button and go back to the property window, here you will see two tabs on the top of this window i.e “Properties” and “Events“. Click on the events tab and find out the “Click”. Double click on the event this will take you to the code file named as MainPage.xaml.cs and into the click event of the button. Here now you code the following :
private void button1_Click(object sender, RoutedEventArgs e)
{
int a,b;
long c;
a = Convert.ToInt16(textBox1.Text);
b = Convert.ToInt16(textBox2.Text);
c = a + b;
MessageBox.Show(c.ToString());
}
Now on top you can see a Play button in green color press that, you will see a emulator booting OS, This is because Emulators are designed with the same configuration as the original phones thats why you must meet the requirements of it. Now your application will run and the layout you designed will be displayed. Provide the values in text boxes and press add, you will see the answer in the message box. if not please go through it again.
Understanding the Event Code
int a,b; long c;
We have declared to Integer variables a and b to collect text box values in them and a long integer variable to collect their Sum.
a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text);
We have to convert the textbox values to integer because the addition operator is performed on numbers and textbox values are of TEXT type or Strings.
c = a + b; MessageBox.Show(c.ToString());
add the values of a and b and place the answer in c. Now displaying the answer c in a message box we have to again convert the c (which has a numeric answer) to String as Message Box is a class which only displays Strings.
Hope you got simple way to work with this post if any queries then leave a comment below.