第二个页面的代码
ForecastPage.xaml
- <phone:PhoneApplicationPage
- x:Class="WeatherForecast.ForecastPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
- xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- SupportedOrientations="Portrait" Orientation="Portrait"
- mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
- shell:SystemTray.IsVisible="True">
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
- <TextBlock x:Name="ApplicationTitle" Text="天气预报" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="{Binding City}" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextNormalStyle}"/>
- </StackPanel>
- <Grid x:Name="ContentGrid" Grid.Row="1" Margin="12,0,12,0">
- <ListBox Height="618" HorizontalAlignment="Left" Margin="0,5,0,0" Name="ForecastList" VerticalAlignment="Top" Width="474" Grid.RowSpan="2" SelectionChanged="ForecastList_SelectionChanged" >
- <ListBox.ItemTemplate><!--数据绑定模板-->
- <DataTemplate>
- <Grid >
- <Grid.RowDefinitions>
- <RowDefinition Height="80"/>
- <RowDefinition Height="80"/>
- <RowDefinition />
- <RowDefinition Height="*" MinHeight="80" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="90" />
- <ColumnDefinition Width="70"/>
- <ColumnDefinition Width="180"/>
- <ColumnDefinition Width="90"/>
- </Grid.ColumnDefinitions>
- <TextBlock Text="{Binding Day_of_week}" Foreground="LightBlue" FontSize="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2"/>
- <Image Source="{Binding Icon}" Grid.Column="0" Grid.Row="0" VerticalAlignment="Bottom" HorizontalAlignment="Right" Grid.ColumnSpan="2" />
- <TextBlock Text="最低温度(K)" FontSize="30" Foreground="LightBlue" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"/>
- <TextBlock Text="{Binding Low}" FontSize="30" Foreground="White" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
- <TextBlock Text="最高温度(K)" FontSize="30" Foreground="LightBlue" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/>
- <TextBlock Text="{Binding High}" FontSize="30" Foreground="White" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
- <TextBlock Text="{Binding Condition}" FontSize="25" Foreground="White" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="4" TextWrapping="Wrap"/>
- </Grid>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>
- using System.Windows.Controls;
- using Microsoft.Phone.Controls;
- using System.Windows.Navigation;
- namespace WeatherForecast
- {
- public partial class ForecastPage : PhoneApplicationPage
- {
- Forecast forecast;
- public ForecastPage()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 当该页面被链接打开时,会调用该事件
- /// </summary>
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- // 获取传过来的City值
- string cityPinyin = this.NavigationContext.QueryString["City"];
- forecast = new Forecast();
- //获取天气类
- forecast.GetForecast(cityPinyin);
- // 设置页面数据绑定到forecast
- DataContext = forecast;
- // 设置ForecastList绑定到forecast.ForecastList
- ForecastList.ItemsSource = forecast.ForecastList;
- }
- private void ForecastList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ForecastList.SelectedIndex = -1;
- ForecastList.SelectedItem = null;
- }
- }
- }
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1085162