namespace Weather { public class Forecast { public string query { get; set; } // cityname, countryname public string observation_time { get; set; } public string date { get; set; } public string temp_C { get; set; } public string tempMaxC { get; set; } public string tempMinC { get; set; } public string weatherIconUrl { get; set; } public string windspeedKmph { get; set; } public string humidity { get; set; } } }
using System.Collections.Generic; namespace Weather { public class PanoramaItemObject { public string observation_time { get; set; } public string date { get; set; } public string temperature { get; set; } public string huminity { get; set; } public string windspeed { get; set; } public string weatherIconUrl { get; set; } // five day's forecast public List<Forecast> forecasts { get; set; } } }
<Grid x:Name="LayoutRoot" Background="Transparent"> <controls:Panorama Title="Weather Forecast" x:Name="Panorama"> <controls:Panorama.TitleTemplate> <DataTemplate> <TextBlock Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" Foreground="White" FontSize="100" Margin="0,60,0,0"/> </DataTemplate> </controls:Panorama.TitleTemplate> <controls:Panorama.Background> <ImageBrush ImageSource="Images/Background3.jpg"/> </controls:Panorama.Background> </controls:Panorama> </Grid>
<DataTemplate x:Key="ForecastsDataTemplate"> <StackPanel Height="40" Orientation="Horizontal" Margin="0,10,0,0"> <TextBlock Text="{Binding date}" FontSize="22" TextAlignment="Left" Width="150"/> <TextBlock Text=" " FontSize="20"/> <Image delay:LowProfileImageLoader.UriSource="{Binding weatherIconUrl}" Width="40" Height="40"/> <TextBlock Text=" " FontSize="20"/> <TextBlock Text="{Binding tempMaxC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/> <TextBlock Text=" " FontSize="20"/> <TextBlock Text="{Binding tempMinC, StringFormat='\{0\} °C'}" FontSize="22" TextAlignment="Right" Width="70"/> </StackPanel> </DataTemplate>
<DataTemplate x:Key="ForecastTemplate"> <Grid x:Name="ContentPanel" Grid.Row="0" Margin="0,-10,0,0"> <Grid Height="150" VerticalAlignment="Top"> <Grid.ColumnDefinitions> <ColumnDefinition Width="150"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Image delay:LowProfileImageLoader.UriSource="{Binding weatherIconUrl}" Width="120" Height="120" Grid.Column="0" VerticalAlignment="Top"/> <StackPanel Grid.Column="1" Height="200" VerticalAlignment="Top"> <TextBlock Text="{Binding temperature}" FontSize="22"/> <TextBlock Text="{Binding observation_time}" FontSize="22"/> <TextBlock Text="{Binding huminity}" FontSize="22"/> <TextBlock Text="{Binding windspeed}" FontSize="22"/> </StackPanel> </Grid> <Grid Height="300" VerticalAlignment="Bottom"> <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="0,0,0,0"> <StackPanel Grid.Row="4" Height="40" Orientation="Horizontal" Margin="0,0,0,0"> <TextBlock Text="Date" FontSize="22" TextAlignment="Left" Width="170"/> <TextBlock Text="FC" FontSize="22" TextAlignment="Left" Width="60"/> <TextBlock Text="Max" FontSize="22" TextAlignment="Right" Width="60"/> <TextBlock Text="Min" FontSize="22" TextAlignment="Right" Width="90"/> </StackPanel> <ListBox ItemTemplate="{StaticResource ForecastsDataTemplate}" ItemsSource="{Binding forecasts}"/> </StackPanel> </Grid> </Grid> </DataTemplate>
xmlns:delay="clr-namespace:Delay;assembly=PhonePerformance"
private ObservableCollection<String> queries = new ObservableCollection<String>(); private int query; private string weatherURL = "http://free.worldweatheronline.com/feed/weather.ashx?q="; private string apiKey; private IsolatedStorageSettings appSettings; const string QueriesSettingsKey = "QueriesKey"; const string APISettingsKey = "APIKey";
// Constructor public MainPage() { InitializeComponent(); // get settings for this application appSettings = IsolatedStorageSettings.ApplicationSettings; // is there network connection available if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { MessageBox.Show("There is no network connection available!"); return; } }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { if (appSettings.Contains(QueriesSettingsKey)) { queries = (ObservableCollection<String>)appSettings[QueriesSettingsKey]; } if (appSettings.Contains(APISettingsKey)) { apiKey = (string)appSettings[APISettingsKey]; } else { apiKey = ""; } // delete old Panorama Items Panorama.Items.Clear(); // start loading weather forecast query = 0; if (queries.Count() > 0 && apiKey != "") LoadForecast(); }
private void LoadForecast() { WebClient downloader = new WebClient(); Uri uri = new Uri(weatherURL + queries.ElementAt(query) + "&format=xml&num_of_days=5&key=" + apiKey, UriKind.Absolute); downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ForecastDownloaded); downloader.DownloadStringAsync(uri); }
private void ForecastDownloaded(object sender, DownloadStringCompletedEventArgs e) { if (e.Result == null || e.Error != null) { MessageBox.Show("Cannot load Weather Forecast!"); } else { XDocument document = XDocument.Parse(e.Result); var data1 = from query in document.Descendants("current_condition") select new Forecast { observation_time = (string) query.Element("observation_time"), temp_C = (string)query.Element("temp_C"), weatherIconUrl = (string)query.Element("weatherIconUrl"), humidity = (string)query.Element("humidity"), windspeedKmph = (string)query.Element("windspeedKmph") }; Forecast forecast = data1.ToList<Forecast>()[0]; var data2 = from query in document.Descendants("weather") select new Forecast { date = (string)query.Element("date"), tempMaxC = (string)query.Element("tempMaxC"), tempMinC = (string)query.Element("tempMinC"), weatherIconUrl = (string)query.Element("weatherIconUrl"), }; List<Forecast> forecasts = data2.ToList<Forecast>(); for (int i = 0; i < forecasts.Count(); i++) { forecasts[i].date = DateTime.Parse(forecasts[i].date).ToString("dddd"); } AddPanoramaItem(forecast,forecasts); } }
private void AddPanoramaItem(Forecast forecast, List<Forecast> forecasts) { // create object to bind the data to UI PanoramaItemObject pio = new PanoramaItemObject(); pio.temperature = "Temperature: " + forecast.temp_C + " °C"; pio.observation_time = "Observ. Time: " + forecast.observation_time; pio.windspeed = "Wind Speed: " + forecast.windspeedKmph + " Kmph"; pio.huminity = "Huminity: " + forecast.humidity + " %"; pio.weatherIconUrl = forecast.weatherIconUrl; pio.forecasts = forecasts; // create PanoramaItem PanoramaItem panoramaItem = new PanoramaItem(); panoramaItem.Header = queries[query]; // modify header to show only city (not the country) int index = queries[query].IndexOf(","); if (index != -1) panoramaItem.Header = queries[query].Substring(0, queries[query].IndexOf(",")); else panoramaItem.Header = queries[query]; // use ForecastTemplate in Panorama Item panoramaItem.ContentTemplate = (DataTemplate)Application.Current.Resources["ForecastTemplate"]; panoramaItem.Content = pio; // add Panorama Item to Panorama Panorama.Items.Add(panoramaItem); // query next city forecast query++; if (query < queries.Count()) LoadForecast(); }
<phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton IconUri="/Images/appbar.feature.settings.rest.png" Text="Settings" Click="Settings_Click"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="Settings" Click="Settings_Click"/> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>
private void Settings_Click(object sender, EventArgs e) { this.NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative)); }
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel Orientation="Vertical"> <TextBlock Text="API Key"/> <TextBox x:Name="APIKey" Text=""/> <TextBlock Text="Add City"/> <TextBox x:Name="NewCityName" Text="Cityname, Countryname"/> <Button Content="Test and Add" Click="Test_Click"/> <TextBlock Text="Cities (click city to remove)"/> <ListBox x:Name="CitiesList" VerticalAlignment="Top" FontSize="30" ItemsSource="{Binding queries}" Height="280" Margin="30,10,0,0" SelectionChanged="CitiesList_SelectionChanged"/> </StackPanel> </Grid>
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { if (appSettings.Contains(QueriesSettingsKey)) { queries = (ObservableCollection<String>)appSettings[QueriesSettingsKey]; } if (appSettings.Contains(APISettingsKey)) { apiKey = (string)appSettings[APISettingsKey]; APIKey.Text = apiKey; } // add cites to CitiesList CitiesList.ItemsSource = queries; }
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { // add queries to isolated storage appSettings.Remove(QueriesSettingsKey); appSettings.Add(QueriesSettingsKey, queries); // add apikey to isolated storage appSettings.Remove(APISettingsKey); appSettings.Add(APISettingsKey, apiKey); }
private void ForecastDownloaded(object sender, DownloadStringCompletedEventArgs e) { if (e.Result == null || e.Error != null) { MessageBox.Show("Cannot load Weather Forecast!"); } else { XDocument document = XDocument.Parse(e.Result); XElement xmlRoot = document.Root; if (xmlRoot.Descendants("error").Count() > 0) { MessageBox.Show("There is no weather forecast available for " + query + " or your apikey is wrong!"); NewCityName.Text = query; } else { queries.Add(query); NewCityName.Text = "Cityname,Countryname"; } } }
private void CitiesList_SelectionChanged(object sender, SelectionChangedEventArgs e) { int selectedIndex = (sender as ListBox).SelectedIndex; if (selectedIndex == -1) return; MessageBoxResult m = MessageBox.Show("Do you want to delete " + queries[selectedIndex] + " from the list?","Delete City?", MessageBoxButton.OKCancel); if (m == MessageBoxResult.OK) { queries.RemoveAt(selectedIndex); } }
Source: https://habr.com/ru/post/138325/