CodeByte

Write your code smartly

Tag Archives: Windows Phone

Get Current Location of Windows Phone Device

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.geoimage

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

Using Camera in Windows Phone

Here i will tell you how to invoke Camera in Windows phone application. Its as simple as you can use a camera in your cell phone.

So now add a button any where in your Windows Phone application from toolbox and double click it to get the click event handler or find the click event from its properties windows show in previous posts.

Please add the following namespace on top of your CS file or code file

using Microsoft.Phone.Tasks;

Add following code on the click event of the button.

private void button1_Click(object sender, RoutedEventArgs e)
{
   CameraCaptureTask cmt = new CameraCaptureTask();
   cmt.Completed += new EventHandler<PhotoResult>(cmt_Completed);
   cmt.Show();
}

The Camera will be launched after clicking button with a dummy Black object moving around a white screen. by pressing the Capture button you can capture the image. You can Play with the image captured in your application like displaying it on image control.

Put the below code in Camera photo result event

void cmt_Completed(object sender, PhotoResult e)
{
   //throw new NotImplementedException();
   BitmapImage bi = new BitmapImage();
   bi.SetSource(e.ChosenPhoto);
   image1.Source = bi;
}

Enjoy Happy Coding !

Simple Calculator in Windows Phone

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.

Create Windows Phone Application

Windows Phone applications are created using C# language on .NET platform 4.0 or above. Simply you must have Visual Studio 2010 service pack 1 installed on your system, if not you can find it fromHere. This down loader will install Windows Phone SDK along with Visual Studio Express limited for Windows phone development. Along with the SDK the Emulator for testing your apps live will also be installed for which you required at-least 2 GB memory. Once you have configured all the setup you are ready to build your first Windows Phone application.

Open Visual Studio Express for Windows Phone and click > File from the menu. Then new Project > Silverlight for Windows Phone .

Now here you can select different type of controls offered by Windows Phone, it includes Panorama, Pivot and simple application. Here how it looks like.

Give the name and location and then press OK. You will see this design view of your first application.

here you can see 2 blocks divided, one is the phone showing the Windows phone and second on the right side is the Silver light code (XAML) generated when you make changes on the phone design by dragging controls from toolbar to the phone. Toolbar is on the left side having common controls . Using these controls you can develop a Windows Phone application. In further posts i will be telling you how you can create a working simple app using XAML and C# code so stay tune and queries you can post comments below.

Happy Coding !

This slideshow requires JavaScript.

Design a site like this with WordPress.com
Get started