Use Silverlight for development in Windows Phone 7

Source: Internet
Author: User
Preface

Windows Phone 7 is now available, and some radical developers are using some developer tools to create a new generation of "Angry Birds" or Netflix applications. As we mentioned in previous articles, Microsoft provides a free developer tool to help developers create Windows Phone applications. The Windows Phone architecture allows developers to choose two different frameworks to create Windows Phone applications/games. In this article, we mainly focus on using the Silverlight framework to create a Windows Phone application.


Why is Silverlight used?

We can't help but ask: why should we use the Silverlight framework to create a Windows Phone application?

Silverlight (we generally think that Silverlight is a direct competitor of Flash) allows Web developers to create rich Internet applications. For programs that require good application logic or practicality, you certainly prefer Silverlight.


Getting started with the Silverlight project in Microsoft Visual Studio

After you install the Windows Phone developer tool, you can directly run Visual Studio Express for Windows Phone. Alternatively, if you have installed Visual Studio 2010 Pro SKUs, you can run Visual Studio 2010 Pro SKUs and select "File"> "New"> "Project ", then select "Silverlight for Windows Phone 7" from the template type ".


Figure 1 select Silverlight for Windows Phone 7

These are some templates you need to know. Depending on the specific application type, you can select the most appropriate template.

For our Demo, We will select the basic "Windows Phone Application" template. Name our project as "WindowsPhoneApplicationDemo" and press "Enter" to create this project.

If we press the "Enter" Key, IDE (Integrated development environment) will perform a series of operations and create a large push file-Appl. xaml, MainPage. xaml and other image files ApplicationIcon.png, Background.png and SplashScreenImage.jpg.

Let's see what these files are:

To View the Designer View, right-click MainPage. xaml and click "View Designer" (if the Designer is unavailable ). This is the designer's appearance and background code:


Figure 2 designer View

For our application, we plan to create a simple calculator that allows users to convert between Celsius and Fahrenheit. To start this operation, press Ctrl + Alt + X to bring up the ControlBox, and then place the two TextBlock controls and the two TextBox controls, drag and Drop a "Button" control to the designer, as shown in:


Figure 3 corresponding controls

Now, let's start customizing our applications.

In MainPage. xaml. find the following code segment in cs, and change the "Text" attribute of "ApplicationTitle" to "My cool convertor ", change the "Text" attribute of "PageTitle" to "Let's rock WP7 ".

 
 
  1. <!--TitlePanel contains the name of the application and page title--> 
  2.          <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  3.              <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> 
  4.              <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  5.          </StackPanel> 
  6.    
  7.  Also change the following snippet in MainPage.xaml.cs as highlighted.  
  8.  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  9.              <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,71,0,0" Name="textBlock1" Text="Degrees" VerticalAlignment="Top" /> 
  10.              <TextBox Height="72" HorizontalAlignment="Left" Margin="162,54,0,0" Name="textBox1" Text="0" VerticalAlignment="Top" Width="245"></TextBox> 
  11.              <TextBlock Height="30" HorizontalAlignment="Left" Margin="29,177,0,0" Name="textBlock2" Text="Fahrenheit" VerticalAlignment="Top" /> 
  12.              <TextBox Height="72" HorizontalAlignment="Left" Margin="162,162,0,0" Name="textBox2" Text="0" VerticalAlignment="Top" Width="245" /> 
  13.              <Button Content="Clear" Height="72" HorizontalAlignment="Left" Margin="139,320,0,0" Name="button1" VerticalAlignment="Top" Width="160" /> 
  14.          </Grid> 

Click the TextBox next to the "Degrees" TextBlock, click "Properties", and double-click the "TextChanged" event to automatically create a new event: "textBox1_TextChanged ". To convert the input value to the Fahrenheit temperature in the TextBox next to the "Fahrenheit" TextBlock, this code should be added:

 
 
  1. private void textBox1_TextChanged(object sender, TextChangedEventArgs e)  
  2.         {  
  3.             if (int.TryParse(textBox1.Text, out degrees))  
  4.             {  
  5.                 fahrenheit = 9 / 5.0 * degrees + 32;  
  6.                 textBox2.Text = fahrenheit.ToString();  
  7.             }  
  8.             else  
  9.                 textBox1.Text = "0";  
  10.               
  11.         } 

You can click the "Build" menu and select "Build Solution" to compile the application. Fix all syntax errors you encounter. Press F5 to start debugging. Note: The first time you start a debugging session, it will be slow. This is because the Windows Phone simulator is loading. We recommend that you do not disable the simulator after debugging. Instead of clicking "Debug"> "Stop Debugging" when you complete the Debugging session ". This ensures that the debugging session load will be faster in the future. When you start the debugger, you will find that your application has been displayed on the simulator.

To start entering the Celsius temperature, click the TextBox next to "Degrees.

When you start typing, you will find that the TextBox next to the "Fahrenheit" TextBlock is automatically updated, which is similar to Google's automatic prompt function.

Click "Clear" to change the Celsius temperature to 0 and the Fahrenheit temperature to 32 (equal to 0 degree Celsius ). If you have trouble creating a project, you can use the sample project in this article.


Summary

In this article, we can see how simple it is to create a simple Silverlight application for Windows Phone. In future articles, I will discuss how to create an XNA-based application for Windows Phone 7.

(This article is reproduced from 51cto, translator Zhou Xuefeng)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.