且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 网络编程之天气预报应用 下

更新时间:2022-10-03 13:57:28

第二个页面的代码

ForecastPage.xaml

 


  1. <phone:PhoneApplicationPage   
  2.     x:Class="WeatherForecast.ForecastPage" 
  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.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  10.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  11.     Foreground="{StaticResource PhoneForegroundBrush}" 
  12.     SupportedOrientations="Portrait" Orientation="Portrait" 
  13.     mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" 
  14.     shell:SystemTray.IsVisible="True"> 
  15.       
  16.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  17.         <Grid.RowDefinitions> 
  18.             <RowDefinition Height="Auto"/> 
  19.             <RowDefinition Height="*"/> 
  20.         </Grid.RowDefinitions> 
  21.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12"> 
  22.             <TextBlock x:Name="ApplicationTitle" Text="天气预报" Style="{StaticResource PhoneTextNormalStyle}"/> 
  23.             <TextBlock x:Name="PageTitle" Text="{Binding City}" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextNormalStyle}"/> 
  24.         </StackPanel> 
  25.         <Grid x:Name="ContentGrid" Grid.Row="1" Margin="12,0,12,0"> 
  26.             <ListBox Height="618" HorizontalAlignment="Left" Margin="0,5,0,0" Name="ForecastList"  VerticalAlignment="Top" Width="474" Grid.RowSpan="2" SelectionChanged="ForecastList_SelectionChanged" > 
  27.                 <ListBox.ItemTemplate><!--数据绑定模板--> 
  28.                     <DataTemplate> 
  29.                         <Grid > 
  30.                             <Grid.RowDefinitions> 
  31.                                 <RowDefinition Height="80"/> 
  32.                                 <RowDefinition Height="80"/> 
  33.                                 <RowDefinition /> 
  34.                                 <RowDefinition Height="*" MinHeight="80" /> 
  35.                             </Grid.RowDefinitions> 
  36.                             <Grid.ColumnDefinitions> 
  37.                                 <ColumnDefinition Width="90" /> 
  38.                                 <ColumnDefinition Width="70"/> 
  39.                                 <ColumnDefinition Width="180"/> 
  40.                                 <ColumnDefinition Width="90"/> 
  41.                             </Grid.ColumnDefinitions> 
  42.                             <TextBlock Text="{Binding Day_of_week}" Foreground="LightBlue"  FontSize="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/> 
  43.                             <Image Source="{Binding Icon}" Grid.Column="0" Grid.Row="0" VerticalAlignment="Bottom" HorizontalAlignment="Right" Grid.ColumnSpan="2" /> 
  44.                             <TextBlock Text="最低温度(K)"  FontSize="30" Foreground="LightBlue" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"/> 
  45.                             <TextBlock Text="{Binding Low}" FontSize="30" Foreground="White" Grid.Column="1" Grid.Row="1"  Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Right"/> 
  46.                             <TextBlock Text="最高温度(K)"  FontSize="30" Foreground="LightBlue" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/> 
  47.                             <TextBlock Text="{Binding High}" FontSize="30" Foreground="White" Grid.Column="1" Grid.Row="2"  Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Right"/> 
  48.                             <TextBlock Text="{Binding Condition}" FontSize="25" Foreground="White" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="4" TextWrapping="Wrap"/> 
  49.  
  50.                         </Grid> 
  51.                     </DataTemplate> 
  52.                 </ListBox.ItemTemplate> 
  53.             </ListBox> 
  54.         </Grid> 
  55.     </Grid> 
  56. </phone:PhoneApplicationPage> 

 


  1. using System.Windows.Controls;  
  2. using Microsoft.Phone.Controls;  
  3. using System.Windows.Navigation;  
  4.  
  5. namespace WeatherForecast  
  6. {  
  7.     public partial class ForecastPage : PhoneApplicationPage  
  8.     {  
  9.         Forecast forecast;  
  10.  
  11.         public ForecastPage()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.  
  16.         /// <summary> 
  17.         /// 当该页面被链接打开时,会调用该事件  
  18.         /// </summary> 
  19.         protected override void OnNavigatedTo(NavigationEventArgs e)  
  20.         {  
  21.             // 获取传过来的City值  
  22.             string cityPinyin = this.NavigationContext.QueryString["City"];  
  23.             forecast = new Forecast();  
  24.             //获取天气类  
  25.             forecast.GetForecast(cityPinyin);  
  26.             // 设置页面数据绑定到forecast  
  27.             DataContext = forecast;  
  28.             // 设置ForecastList绑定到forecast.ForecastList  
  29.             ForecastList.ItemsSource = forecast.ForecastList;  
  30.         }  
  31.  
  32.         private void ForecastList_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  33.         {  
  34.             ForecastList.SelectedIndex = -1;  
  35.             ForecastList.SelectedItem = null;  
  36.  
  37.         }  
  38.     }  

 



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1085162