-
-
Save OliPicard/654b7bd5c1dad9803929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace Weather | |
{ | |
[JsonConverter(typeof(QueryResultConverter))] | |
public class QueryResult | |
{ | |
public List<Forecast> Forecasts { get; set; } | |
public string WindDirection { get; set; } | |
} | |
public class QueryResultConverter : JsonConverter | |
{ | |
public override bool CanConvert(Type objectType) | |
{ | |
return (objectType == typeof(QueryResult)); //new boolean overriding objectType with LineDiplay | |
} | |
public override bool CanWrite { get { return false; } } | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | |
{ | |
JObject jo = JObject.Load(reader); //new JObject reader (custom ReadJson Deseralizser) | |
var result = new QueryResult | |
{ | |
WindDirection = (string)jo.SelectToken("query.results.channel.wind.direction"), | |
Forecasts = jo.SelectToken("query.results.channel.item.forecast").ToObject<List<Forecast>>() | |
}; | |
return result; | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="Weather.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:converters="clr-namespace:Weather.Converters" | |
Title="Weather" Height="350" Width="525" ResizeMode="NoResize"> | |
<Window.Resources> | |
<converters:ConvertModels x:Key="ConvertModels" /> | |
<converters:WindConverter x:Key="WindConverter" /> | |
</Window.Resources> | |
<Grid> | |
<Label x:Name="texty" Content="" HorizontalAlignment="Left" Margin="9,235,0,0" VerticalAlignment="Top" FontSize="48"/> | |
<Label x:Name="textyfresh" Content="" HorizontalAlignment="Left" Margin="69,84,0,0" VerticalAlignment="Top" FontSize="15"/> | |
<Label x:Name="Sun" Content="" HorizontalAlignment="Left" Margin="69,119,0,0" VerticalAlignment="Top" FontSize="24"/> | |
<TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="London" VerticalAlignment="Top" Width="120" KeyDown="EnterKey"/> | |
<Button x:Name="go" Content="Go" HorizontalAlignment="Left" Margin="121,1,0,0" VerticalAlignment="Top" Width="75" Click="go_Click"/> | |
<DataGrid x:Name="Grid1" IsReadOnly="True" HorizontalAlignment="Left" Margin="289,169,0,0" VerticalAlignment="Top" AutoGenerateColumns="False" Width="218" Height="128" ClipToBounds="True" Background="White"> | |
<DataGrid.Columns> | |
<DataGridTextColumn Header="day" Binding="{Binding day}"/> | |
<DataGridTextColumn Header="high" Binding="{Binding high}" /> | |
<DataGridTextColumn Header="low" Binding="{Binding low}" /> | |
<DataGridTextColumn Header="condtion" Binding="{Binding text}" Width="*" /> | |
</DataGrid.Columns> | |
</DataGrid> | |
<Label Content="Today's Weather" HorizontalAlignment="Left" Margin="52,53,0,0" VerticalAlignment="Top" FontWeight="Bold"/> | |
<Label Content="This week's weather" HorizontalAlignment="Left" Margin="342,136,0,0" VerticalAlignment="Top" FontWeight="Bold"/> | |
<Label x:Name="SRise" Content="" HorizontalAlignment="Left" Margin="385,65,0,0" VerticalAlignment="Top"/> | |
<Label x:Name="SSet" Content="" HorizontalAlignment="Left" Margin="385,87,0,0" VerticalAlignment="Top"/> | |
<Label Content="Sun Rise" HorizontalAlignment="Left" Margin="317,65,0,0" VerticalAlignment="Top"/> | |
<Label Content="Sun Set" HorizontalAlignment="Left" Margin="322,87,0,0" VerticalAlignment="Top"/> | |
<Image x:Name="img" Source="{Binding Path=Condition, Converter={StaticResource ConvertModels}}" HorizontalAlignment="Left" Height="107" Margin="186,21,0,0" VerticalAlignment="Top" Width="136"></Image> | |
<Image x:Name="wind" Source="{Binding Path=WindDirection, Converter={StaticResource WindConverter}}" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Margin="164,197,0,0"/> | |
<Label Content="Label" HorizontalAlignment="Left" Margin="81,285,0,0" VerticalAlignment="Top"/> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.TextFormatting; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Net.Http; | |
using Newtonsoft.Json; | |
using System.ComponentModel; | |
namespace Weather | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window, INotifyPropertyChanged | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
Get(); | |
DataContext = this; | |
} | |
string _direction { get; set; } | |
public string Direction //this model is being bound at xaml level. | |
{ | |
get | |
{ | |
return _direction; | |
} | |
set | |
{ | |
_direction = value; //sets condition for value convertmodels | |
OnPropertyChanged("WindDirection"); //condition changed. | |
} | |
} | |
string _condition { get; set; } | |
public string Condition //this model is being bound at xaml level. | |
{ | |
get | |
{ | |
return _condition; | |
} | |
set | |
{ | |
_condition = value; //sets condition for value convertmodels | |
OnPropertyChanged("Condition"); //condition changed. | |
} | |
} | |
async private void Get() | |
{ | |
using (HttpClient client = new HttpClient()) | |
{ | |
string a = textbox1.Text; | |
string Query = ("select%20*%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text=%22%s%22%29%20and%20u=%27c%27&format=json"); | |
string Final = Query.Replace("%s", a); | |
string Url = ("https://query.yahooapis.com/v1/public/yql?q=" + Final); | |
var response = await client.GetAsync(new Uri(Url)); | |
if (response.IsSuccessStatusCode) | |
{ | |
var content = await response.Content.ReadAsStringAsync(); | |
QueryResult Result = JsonConvert.DeserializeObject<QueryResult>(content); | |
string Name = Result.City; | |
string Forty = Result.Condition; | |
string Windy = Result.Direction; | |
string Temp = Result.Temperature.ToString(); //force that temp to string. | |
//List<Weather.Forecast> example = result.Forecasts; | |
string SunRising = Result.SunRise; | |
Condition = Result.Condition; //sets up the condition for convertmodels to use | |
Direction = Result.Direction; | |
string SunSetting = Result.SunSet; | |
var Example = Result.Forecasts; //populating forecast grid. | |
Grid1.ItemsSource = Example; //binding dat data | |
textyfresh.Content = Forty; | |
texty.Content = Name; | |
SRise.Content = SunRising; | |
SSet.Content = SunSetting; | |
int Jack = Convert.ToInt32(Temp); | |
if (Jack > 30) | |
{ | |
Sun.Content = "Very Hot" + Jack + "c"; | |
Sun.Foreground = new System.Windows.Media.SolidColorBrush(Colors.OrangeRed); | |
} | |
else if (Jack > 25) | |
{ | |
Sun.Content = "Warm! " + Jack + "c"; | |
} | |
else if (Jack > 10) | |
{ | |
Sun.Content = "Mild " + Jack + "c"; | |
Sun.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Navy); | |
} | |
else | |
{ | |
Sun.Content = Jack + "c"; | |
} | |
} | |
else | |
{ | |
texty.Content = "Error"; | |
} | |
} | |
} | |
private void go_Click(object sender, RoutedEventArgs e) | |
{ | |
Get(); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected virtual void OnPropertyChanged(string propertyName) | |
{ | |
var handler = PropertyChanged; | |
if (handler != null) | |
{ | |
handler(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
private void EnterKey(object sender, KeyEventArgs e) | |
{ | |
if (e.Key == Key.Return) | |
{ | |
Get(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Windows.Data; | |
namespace Weather.Converters | |
{ | |
public class WindConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
var valuing = (string) value; | |
int windstring; | |
bool knock = Int32.TryParse(valuing, out windstring); | |
if (knock) | |
{ | |
if (windstring <= 22.5 || windstring > 337.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
else if (windstring > 22.5 || windstring <= 67.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
else if (windstring > 67.5 || windstring <= 112.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
else if (windstring > 112.5 || windstring <= 157.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
else if (windstring > 157.5 || windstring <= 202.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
else if (windstring > 247.5 || windstring <= 292.5) | |
{ | |
return "pack://application:,,,/Images/rain.png"; | |
} | |
} | |
return null; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment