Sample Country Application In Windows Phone 7

Xây dựng ứng dụng đơn giản với Windows Phone 7

I.Introduction

-Trong bài này mình xin chia sẻ cách tạo một ứng dụng đơn giản trên Windows Phone 7.

-Chúng ta sẽ làm một ứng dụng cho phép chọn một quốc gia, và Show thông tin quốc gia đó….

II.Creating Project And Coding

-Mở Visual Studio 2010 lên và tạo Windows Phone Application Project

image

-Bây giờ chúng ta làm giao diện cho ứng dụng như đoạn XAML sau :

   1: <phone:PhoneApplicationPage 

   2:     x:Class="CountryApp.MainPage"

   3:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   4:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   5:     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

   6:     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

   7:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   8:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   9:     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

  10:     FontFamily="{StaticResource PhoneFontFamilyNormal}"

  11:     FontSize="{StaticResource PhoneFontSizeNormal}"

  12:     Foreground="{StaticResource PhoneForegroundBrush}"

  13:     SupportedOrientations="Portrait" Orientation="Portrait"

  14:     shell:SystemTray.IsVisible="True">

  15:  

  16:     <!--LayoutRoot is the root grid where all page content is placed-->

  17:     <Grid x:Name="LayoutRoot" Background="Transparent">

  18:         <Grid.RowDefinitions>

  19:             <RowDefinition Height="Auto"/>

  20:             <RowDefinition Height="*"/>

  21:         </Grid.RowDefinitions>

  22:  

  23:         <!--TitlePanel contains the name of the application and page title-->

  24:         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

  25:             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

  26:             <TextBlock x:Name="PageTitle" Text="Country App" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

  27:         </StackPanel>

  28:  

  29:         <!--ContentPanel - place additional content here-->

  30:         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

  31:             <Grid.RowDefinitions>

  32:                 <RowDefinition Height="60*" />

  33:                 <RowDefinition Height="114*" />

  34:                 <RowDefinition Height="433*" />

  35:             </Grid.RowDefinitions>

  36:             <ComboBox Grid.Row="0" Name="cbx_Country">

  37:                 <ComboBox.ItemTemplate>

  38:                     <DataTemplate>

  39:                         <TextBlock Text="{Binding Name}" Foreground="Black"/>

  40:                     </DataTemplate>

  41:                 </ComboBox.ItemTemplate>

  42:             </ComboBox>

  43:             <TextBlock Grid.Row="1" Name="lbl_Country" Style="{StaticResource PhoneTextTitle1Style}"/>

  44:             <TextBlock Grid.Row="2" Height="30" HorizontalAlignment="Left" Margin="12,48,0,0" Name="textBlock1" Text="Capital : " VerticalAlignment="Top" />

  45:             <TextBox Grid.Row="2" Height="72" HorizontalAlignment="Left" Margin="103,27,0,0" Name="txt_Capital" Text="" VerticalAlignment="Top" Width="353" />

  46:             <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,126,0,0" Name="textBlock2" Text="Language : " VerticalAlignment="Top" Grid.Row="2" />

  47:             <TextBox Height="72" HorizontalAlignment="Left" Margin="103,105,0,0" Name="txt_Language" Text="" VerticalAlignment="Top" Width="353" Grid.Row="2" />

  48:             <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,204,0,0" Name="textBlock3" Text="Currency : " VerticalAlignment="Top" Grid.Row="2" />

  49:             <TextBox Height="72" HorizontalAlignment="Left" Margin="103,183,0,0" Name="txt_Currency" Text="" VerticalAlignment="Top" Width="353" Grid.Row="2" />

  50:         </Grid>

  51:         <StackPanel Grid.Row="1" Height="21" HorizontalAlignment="Left" Margin="12,174,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="456">

  52:             <StackPanel.Background>

  53:                 <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">

  54:                     <GradientStop Color="Black" Offset="0" />

  55:                     <GradientStop Color="White" Offset="1" />

  56:                 </LinearGradientBrush>

  57:             </StackPanel.Background>

  58:         </StackPanel>

  59:     </Grid>

  60:  

  61:     <!--Sample code showing usage of ApplicationBar-->

  62:     <!--<phone:PhoneApplicationPage.ApplicationBar>

  63:         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">

  64:             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>

  65:             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>

  66:             <shell:ApplicationBar.MenuItems>

  67:                 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>

  68:                 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>

  69:             </shell:ApplicationBar.MenuItems>

  70:         </shell:ApplicationBar>

  71:     </phone:PhoneApplicationPage.ApplicationBar>-->

  72:  

  73: </phone:PhoneApplicationPage>

– Bây giờ Chúng ta tạo một Database XML để lưu dữ liệu:

   1: <?xml version="1.0" encoding="utf-8" ?>

   2: <Countries>

   3:   <Country>

   4:     <Name>Viet Nam</Name>

   5:     <Capital>Ha Noi</Capital>

   6:     <Language>VietNamese</Language>

   7:     <Currency>VND</Currency>

   8:   </Country>

   9:   <Country>

  10:     <Name>USA</Name>

  11:     <Capital>Washinton DC</Capital>

  12:     <Language>English</Language>

  13:     <Currency>Dollars</Currency>

  14:   </Country>

  15:   <Country>

  16:     <Name>England</Name>

  17:     <Capital>London </Capital>

  18:     <Language>English</Language>

  19:     <Currency>Pounds</Currency>

  20:   </Country>

  21:   <Country>

  22:     <Name>France</Name>

  23:     <Capital>Paris </Capital>

  24:     <Language>French</Language>

  25:     <Currency>Euro</Currency>

  26:   </Country>

  27:   <Country>

  28:     <Name>Germany</Name>

  29:     <Capital>Berlin </Capital>

  30:     <Language>German</Language>

  31:     <Currency>Mark</Currency>

  32:   </Country>

  33:   <Country>

  34:     <Name>Russia</Name>

  35:     <Capital>Moscow </Capital>

  36:     <Language>Russian</Language>

  37:     <Currency>Not Availaible</Currency>

  38:   </Country>

  39:   <Country>

  40:     <Name>Spain</Name>

  41:     <Capital>Madrid </Capital>

  42:     <Language>Spanish</Language>

  43:     <Currency>Not Availaible</Currency>

  44:   </Country>

  45:   <Country>

  46:     <Name>Turkey</Name>

  47:     <Capital>Ankara </Capital>

  48:     <Language>Not Availaible</Language>

  49:     <Currency>Not Availaible</Currency>

  50:   </Country>

  51:   <Country>

  52:     <Name>Norway</Name>

  53:     <Capital>Oslo </Capital>

  54:     <Language>Not Availaible</Language>

  55:     <Currency>Not Availaible</Currency>

  56:   </Country>

  57:   <Country>

  58:     <Name>Canada</Name>

  59:     <Capital>Ottawa </Capital>

  60:     <Language>English</Language>

  61:     <Currency>Dollars</Currency>

  62:   </Country>

  63:   <Country>

  64:     <Name>Mexico</Name>

  65:     <Capital>Mexico </Capital>

  66:     <Language>Spanish</Language>

  67:     <Currency>Peso</Currency>

  68:   </Country>

  69:   <Country>

  70:     <Name>China</Name>

  71:     <Capital>Bejing </Capital>

  72:     <Language>Chinnes</Language>

  73:     <Currency>Not Availaible</Currency>

  74:   </Country>

  75:   <Country>

  76:     <Name>Japan</Name>

  77:     <Capital>Tokyo </Capital>

  78:     <Language>Jaopanese</Language>

  79:     <Currency>Yen</Currency>

  80:   </Country>

  81:   <Country>

  82:     <Name>India</Name>

  83:     <Capital>New Delhi </Capital>

  84:     <Language>Hindi</Language>

  85:     <Currency>Ruppies</Currency>

  86:   </Country>

  87:   <Country>

  88:     <Name>Australia</Name>

  89:     <Capital>Canberra </Capital>

  90:     <Language>English</Language>

  91:     <Currency>Dollars</Currency>

  92:   </Country>

  93: </Countries>

Tiếp theo chúng ta xây dựng một Lớp tương ứng và phương thức lấy dữ liệu của lớp :
   1: using System;

   2: using System.Net;

   3: using System.Windows;

   4: using System.Windows.Controls;

   5: using System.Windows.Documents;

   6: using System.Windows.Ink;

   7: using System.Windows.Input;

   8: using System.Windows.Media;

   9: using System.Windows.Media.Animation;

  10: using System.Windows.Shapes;

  11: using System.Collections.Generic;

  12: using System.Xml.Linq;

  13: using System.Linq;

  14: namespace CountryApp

  15: {

  16:     public class Country

  17:     {

  18:         public string Name { get; set; }

  19:         public string Capital { get; set; }

  20:         public string Language { get; set; }

  21:         public string Currency { get; set; }

  22:         public Country()

  23:         { }

  24:         public List<Country> GetData()

  25:         {

  26:             XDocument Data = XDocument.Load("XMLCountryFile.xml");

  27:             var mycountry = from r in Data.Descendants("Country")

  28:                             select new Country

  29:                             {

  30:                                 Name = r.Element("Name").Value,

  31:                                 Language = r.Element("Language").Value,

  32:                                 Capital = r.Element("Capital").Value,

  33:                                 Currency = r.Element("Currency").Value

  34:                             };

  35:             return mycountry.ToList();

  36:         }

  37:  

  38:     }

  39: }

Cuối cùng chúng ta quay lại MainPage để thực xử lý sự kiện SelectionChanged :

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Linq;

   4: using System.Net;

   5: using System.Windows;

   6: using System.Windows.Controls;

   7: using System.Windows.Documents;

   8: using System.Windows.Input;

   9: using System.Windows.Media;

  10: using System.Windows.Media.Animation;

  11: using System.Windows.Shapes;

  12: using Microsoft.Phone.Controls;

  13:  

  14: namespace CountryApp

  15: {

  16:     public partial class MainPage : PhoneApplicationPage

  17:     {

  18:         // Constructor

  19:         Country myCountry = new Country();

  20:         public MainPage()

  21:         {

  22:             InitializeComponent();

  23:             this.cbx_Country.SelectionChanged += 

  24:                 new SelectionChangedEventHandler(cbx_Country_SelectionChanged);

  25:             cbx_Country.ItemsSource = myCountry.GetData();

  26:             cbx_Country.SelectedIndex = 0;

  27:         }

  28:  

  29:         void cbx_Country_SelectionChanged(object sender, SelectionChangedEventArgs e)

  30:         {

  31:             string Name = (cbx_Country.SelectedItem as Country).Name;

  32:             foreach (Country item in myCountry.GetData())

  33:             {

  34:                 if (item.Name == Name)

  35:                 {

  36:                     lbl_Country.Text = item.Name;

  37:                     txt_Capital.Text = item.Capital;

  38:                     txt_Language.Text = item.Language;

  39:                     txt_Currency.Text = item.Currency;

  40:                 }

  41:             }

  42:         }

  43:     }

  44: }

Cuối cùng chạy ứng dụng và xem kết quả :

image
image
/*Life run on code*/

Leave a comment