Added WeatherApp Sample
This commit is contained in:
		
							parent
							
								
									4248652123
								
							
						
					
					
						commit
						de10d8a179
					
				| 
						 | 
				
			
			@ -33,6 +33,7 @@ namespace Samples
 | 
			
		|||
            new DotMatrixClockSample().Publish();
 | 
			
		||||
            new EditorSample().Publish();
 | 
			
		||||
            new TipCalcSample().Publish();
 | 
			
		||||
            new WeatherAppSample().Publish();
 | 
			
		||||
            new XuzzleSample().Publish();
 | 
			
		||||
 | 
			
		||||
            UI.Present ("/display-alert");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,6 +44,9 @@
 | 
			
		|||
    <EmbeddedResource Update="TipCalc\TipCalcPage.xaml">
 | 
			
		||||
      <Generator>MSBuild:Compile</Generator>
 | 
			
		||||
    </EmbeddedResource>
 | 
			
		||||
    <EmbeddedResource Update="WeatherApp\WeatherPage.xaml">
 | 
			
		||||
      <Generator>MSBuild:Compile</Generator>
 | 
			
		||||
    </EmbeddedResource>
 | 
			
		||||
    <EmbeddedResource Update="XamlPreviewPage.xaml">
 | 
			
		||||
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
 | 
			
		||||
    </EmbeddedResource>
 | 
			
		||||
| 
						 | 
				
			
			@ -54,6 +57,7 @@
 | 
			
		|||
    <None Remove="BugSweeper\Images\Xamarin120.png" />
 | 
			
		||||
    <None Remove="DotMatrixClock\DotMatrixClockPage.xaml" />
 | 
			
		||||
    <None Remove="TipCalc\TipCalcPage.xaml" />
 | 
			
		||||
    <None Remove="WeatherApp\WeatherPage.xaml" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <PropertyGroup>
 | 
			
		||||
    <OutputType>Exe</OutputType>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
using System;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace WeatherApp
 | 
			
		||||
{
 | 
			
		||||
    public class Core
 | 
			
		||||
    {
 | 
			
		||||
        public static async Task<Weather> GetWeather(string zipCode)
 | 
			
		||||
        {
 | 
			
		||||
            //Sign up for a free API key at http://openweathermap.org/appid
 | 
			
		||||
            string key = "fc9f6c524fc093759cd28d41fda89a1b";
 | 
			
		||||
            string queryString = "http://api.openweathermap.org/data/2.5/weather?zip="
 | 
			
		||||
                + zipCode + "&appid=" + key;
 | 
			
		||||
 | 
			
		||||
            var results = await DataService.getDataFromService(queryString).ConfigureAwait(false);
 | 
			
		||||
 | 
			
		||||
            if (results["weather"] != null)
 | 
			
		||||
            {
 | 
			
		||||
                Weather weather = new Weather
 | 
			
		||||
                {
 | 
			
		||||
                    Title = (string)results["name"],
 | 
			
		||||
                    Temperature = (string)results["main"]["temp"] + " F",
 | 
			
		||||
                    Wind = (string)results["wind"]["speed"] + " mph",
 | 
			
		||||
                    Humidity = (string)results["main"]["humidity"] + " %",
 | 
			
		||||
                    Visibility = (string)results["weather"][0]["main"]
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
                DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
 | 
			
		||||
                DateTime sunrise = time.AddSeconds((double)results["sys"]["sunrise"]);
 | 
			
		||||
                DateTime sunset = time.AddSeconds((double)results["sys"]["sunset"]);
 | 
			
		||||
                weather.Sunrise = sunrise.ToString() + " UTC";
 | 
			
		||||
                weather.Sunset = sunset.ToString() + " UTC";
 | 
			
		||||
                return weather;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
using System.Threading.Tasks;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using Newtonsoft.Json.Linq;
 | 
			
		||||
 | 
			
		||||
namespace WeatherApp
 | 
			
		||||
{
 | 
			
		||||
    public class DataService
 | 
			
		||||
    {
 | 
			
		||||
        public static async Task<JContainer> getDataFromService(string queryString)
 | 
			
		||||
        {
 | 
			
		||||
            HttpClient client = new HttpClient();
 | 
			
		||||
            var response = await client.GetAsync(queryString);
 | 
			
		||||
 | 
			
		||||
            JContainer data = null;
 | 
			
		||||
            if (response != null)
 | 
			
		||||
            {
 | 
			
		||||
                string json = response.Content.ReadAsStringAsync().Result;
 | 
			
		||||
                data = (JContainer)JsonConvert.DeserializeObject(json);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return data;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
namespace WeatherApp
 | 
			
		||||
{
 | 
			
		||||
    public class Weather
 | 
			
		||||
    {
 | 
			
		||||
        public string Title { get; set; }
 | 
			
		||||
        public string Temperature { get; set; }
 | 
			
		||||
        public string Wind { get; set; }
 | 
			
		||||
        public string Humidity { get; set; }
 | 
			
		||||
        public string Visibility { get; set; }
 | 
			
		||||
        public string Sunrise { get; set; }
 | 
			
		||||
        public string Sunset { get; set; }
 | 
			
		||||
 | 
			
		||||
        public Weather()
 | 
			
		||||
        {
 | 
			
		||||
            //Because labels bind to these values, set them to an empty string to
 | 
			
		||||
            //ensure that the label appears on all platforms by default.
 | 
			
		||||
            this.Title = " ";
 | 
			
		||||
            this.Temperature = " ";
 | 
			
		||||
            this.Wind = " ";
 | 
			
		||||
            this.Humidity = " ";
 | 
			
		||||
            this.Visibility = " ";
 | 
			
		||||
            this.Sunrise = " ";
 | 
			
		||||
            this.Sunset = " ";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,92 @@
 | 
			
		|||
<?xml version="1.0" encoding="utf-8" ?>
 | 
			
		||||
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
 | 
			
		||||
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
 | 
			
		||||
       x:Class="WeatherApp.WeatherPage">
 | 
			
		||||
 | 
			
		||||
  <ContentPage.Resources>
 | 
			
		||||
    <ResourceDictionary>
 | 
			
		||||
      <Style x:Key="labelStyle" TargetType="Label">
 | 
			
		||||
        <Setter Property="TextColor" Value="#a8a8a8" />
 | 
			
		||||
        <Setter Property="FontSize" Value="Small" />
 | 
			
		||||
      </Style>
 | 
			
		||||
      <Style x:Key="fieldStyle" TargetType="Label">
 | 
			
		||||
        <Setter Property="TextColor">
 | 
			
		||||
          <OnPlatform x:TypeArguments="Color">
 | 
			
		||||
              <On Platform="iOS" Value="Black" />
 | 
			
		||||
              <On Platform="Android, UWP, WinRT, WinPhone" Value="White" />
 | 
			
		||||
          </OnPlatform>
 | 
			
		||||
        </Setter>
 | 
			
		||||
        <Setter Property="FontSize" Value="Medium" />
 | 
			
		||||
      </Style>
 | 
			
		||||
      <Style x:Key="fieldView" TargetType="ContentView">
 | 
			
		||||
        <Setter Property="Padding" Value="10,0,0,0" />
 | 
			
		||||
      </Style>
 | 
			
		||||
    </ResourceDictionary>
 | 
			
		||||
  </ContentPage.Resources>
 | 
			
		||||
 | 
			
		||||
  <ContentPage.Content>
 | 
			
		||||
    <ScrollView>
 | 
			
		||||
      <StackLayout>
 | 
			
		||||
        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"
 | 
			
		||||
              BackgroundColor="#545454">
 | 
			
		||||
          <StackLayout Padding="10,10,10,10" HorizontalOptions="Start">
 | 
			
		||||
            <Label Text="Search by Zip Code" TextColor="White" FontAttributes="Bold"
 | 
			
		||||
                FontSize="Medium" />
 | 
			
		||||
            <Label x:Name="zipCodeLabel" Text="Zip Code" Style="{StaticResource labelStyle}" />
 | 
			
		||||
            <Entry x:Name="zipCodeEntry" />
 | 
			
		||||
          </StackLayout>
 | 
			
		||||
          <StackLayout Padding="0,0,0,10" VerticalOptions="End">
 | 
			
		||||
            <Button x:Name="getWeatherBtn" Text="Get Weather" WidthRequest="185" BorderWidth="1" >
 | 
			
		||||
              <!-- Set iOS colors; use defaults on other platforms -->
 | 
			
		||||
              <Button.TextColor>
 | 
			
		||||
                <OnPlatform x:TypeArguments="Color">
 | 
			
		||||
                    <On Platform="iOS" Value="White" />
 | 
			
		||||
                </OnPlatform>
 | 
			
		||||
              </Button.TextColor>
 | 
			
		||||
              <Button.BorderColor>
 | 
			
		||||
                <OnPlatform x:TypeArguments="Color">
 | 
			
		||||
                    <On Platform="iOS" Value="White" />
 | 
			
		||||
                </OnPlatform>
 | 
			
		||||
              </Button.BorderColor>
 | 
			
		||||
            </Button>
 | 
			
		||||
          </StackLayout>
 | 
			
		||||
        </StackLayout>
 | 
			
		||||
        <StackLayout Padding="10,10,10,10" HorizontalOptions="Start">
 | 
			
		||||
          <Label Text="Location" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label Text="{Binding Title}" Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Temperature" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="tempLabel" Text="{Binding Temperature}"
 | 
			
		||||
                Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Wind Speed" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="windLabel" Text="{Binding Wind}" Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Humidity" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="humidityLabel" Text="{Binding Humidity}"
 | 
			
		||||
                Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Visibility" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="visibilitylabel" Text="{Binding Visibility}"
 | 
			
		||||
                Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Time of Sunrise" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="sunriseLabel" Text="{Binding Sunrise}"
 | 
			
		||||
                Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
          <Label Text="Time of Sunset" Style="{StaticResource labelStyle}" />
 | 
			
		||||
          <ContentView Style="{StaticResource fieldView}">
 | 
			
		||||
            <Label x:Name="sunsetLabel" Text="{Binding Sunset}"
 | 
			
		||||
                Style="{StaticResource fieldStyle}" />
 | 
			
		||||
          </ContentView>
 | 
			
		||||
        </StackLayout>
 | 
			
		||||
      </StackLayout>
 | 
			
		||||
    </ScrollView>
 | 
			
		||||
  </ContentPage.Content>
 | 
			
		||||
</ContentPage>
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,31 @@
 | 
			
		|||
using System;
 | 
			
		||||
using Xamarin.Forms;
 | 
			
		||||
 | 
			
		||||
namespace WeatherApp
 | 
			
		||||
{
 | 
			
		||||
    public partial class WeatherPage : ContentPage
 | 
			
		||||
    {
 | 
			
		||||
        public WeatherPage()
 | 
			
		||||
        {
 | 
			
		||||
            InitializeComponent();
 | 
			
		||||
            this.Title = "Sample Weather App";
 | 
			
		||||
            getWeatherBtn.Clicked += GetWeatherBtn_Clicked;
 | 
			
		||||
 | 
			
		||||
            //Set the default binding to a default object for now
 | 
			
		||||
            this.BindingContext = new Weather();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private async void GetWeatherBtn_Clicked(object sender, EventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            if (!String.IsNullOrEmpty(zipCodeEntry.Text))
 | 
			
		||||
            {
 | 
			
		||||
                Weather weather = await Core.GetWeather(zipCodeEntry.Text);
 | 
			
		||||
                if (weather != null)
 | 
			
		||||
                {
 | 
			
		||||
                    this.BindingContext = weather;
 | 
			
		||||
                    getWeatherBtn.Text = "Search Again";
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,21 @@
 | 
			
		|||
using Ooui;
 | 
			
		||||
using Xamarin.Forms;
 | 
			
		||||
 | 
			
		||||
namespace Samples
 | 
			
		||||
{
 | 
			
		||||
    public class WeatherAppSample : ISample
 | 
			
		||||
    {
 | 
			
		||||
        public string Title => "Xamarin.Forms WeatherApp";
 | 
			
		||||
 | 
			
		||||
        public Ooui.Element CreateElement()
 | 
			
		||||
        {
 | 
			
		||||
            var page = new WeatherApp.WeatherPage();
 | 
			
		||||
            return page.GetOouiElement();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Publish()
 | 
			
		||||
        {
 | 
			
		||||
            UI.Publish("/weatherapp", CreateElement);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue