Advertisement
eduardogr

Untitled

Oct 28th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1.  <Page.Resources>
  2.         <vm:WeatherVM x:Key="vm" />
  3.         <c:ValuesConverter x:Key="cv" />
  4.     </Page.Resources>
  5.  
  6.     <RelativePanel DataContext="{StaticResource vm}">
  7.         <TextBox x:Name="currentLocation"
  8.                  PlaceholderText="Please wait..."
  9.                  IsReadOnly="True"
  10.                  Margin="20"
  11.                  Width="300"
  12.                  Text="{Binding cityData,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
  13.         <ListView RelativePanel.Below="currentLocation"
  14.                   x:Name="ForecastList"
  15.                   Margin="20"
  16.                   SelectedItem="{Binding currentDay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  17.                   ItemsSource="{Binding dailyForecasts}">
  18.             <ListView.ItemTemplate>
  19.                 <DataTemplate>
  20.                     <StackPanel Margin="10">
  21.                         <TextBlock x:Name="dateTB"
  22.                                    Text="{Binding Date.DayOfWeek}" />
  23.                         <TextBlock x:Name="highTB"
  24.                                    Text="{Binding Temperature.Maximum.Value, Converter={StaticResource cv}}"
  25.                                    FontSize="10" />
  26.                         <TextBlock x:Name="lowTB"
  27.                                    FontSize="10"
  28.                                    Text="{Binding Temperature.Minimum.Value, Converter={StaticResource cv}}" />
  29.                     </StackPanel>
  30.                 </DataTemplate>
  31.             </ListView.ItemTemplate>
  32.         </ListView>
  33.         <ProgressRing x:Name="pRing"
  34.                       RelativePanel.Above="ForecastList"
  35.                       RelativePanel.AlignHorizontalCenterWith="currentLocation"
  36.                      IsActive="{Binding ProgressRingIsActive, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
  37.                       Margin="0,0,0,-23" />
  38.         <Image x:Name="Icon"
  39.                Source="{Binding currentDay.Day.IconUrl}"
  40.                Width="50"
  41.                Height="50"
  42.                RelativePanel.AlignVerticalCenterWithPanel="True"
  43.                RelativePanel.AlignHorizontalCenterWithPanel="True"
  44.                RelativePanel.AlignTopWith="ForecastList" />
  45.         <TextBlock x:Name="hight_temp"
  46.                    Text="High:"
  47.                    FontSize="20"
  48.                    RelativePanel.AlignVerticalCenterWithPanel="True"
  49.                    RelativePanel.AlignHorizontalCenterWithPanel="True"
  50.                    RelativePanel.Below="Icon"
  51.                    RelativePanel.AlignLeftWith="Icon" />
  52.         <TextBlock Text="{Binding currentDay.Temperature.Maximum.Value, Converter={StaticResource cv}}"
  53.                    x:Name="high"
  54.                    FontSize="20"
  55.                    RelativePanel.RightOf="hight_temp"
  56.                    RelativePanel.Below="Icon" />
  57.         <TextBlock x:Name="low_temp"
  58.                    RelativePanel.Below="hight_temp"
  59.                    Text="Low: "
  60.                    RelativePanel.AlignLeftWith="hight_temp"
  61.                    FontSize="20" />
  62.         <TextBlock x:Name="low"
  63.                    Text="{Binding currentDay.Temperature.Minimum.Value, Converter={StaticResource cv}}"
  64.                    FontSize="20"
  65.                    RelativePanel.Below="high"
  66.                    RelativePanel.AlignRightWith="high"
  67.                    RelativePanel.RightOf="low_temp" />
  68.         <TextBlock x:Name="desc"
  69.                    Text="{Binding currentDay.Day.IconPhrase}"
  70.                    RelativePanel.Below="low_temp"
  71.                    RelativePanel.AlignHorizontalCenterWithPanel="True"
  72.                    FontSize="20"
  73.                    FontWeight="Bold"
  74.                    RelativePanel.AlignLeftWith="low_temp" />
  75.     </RelativePanel>
  76.  
  77.  
  78. </Page>
  79.  
  80.   public AccuWeather accuWeather { get; set; }
  81.  
  82.         private string _cityData;
  83.         public string cityData {
  84.             get { return _cityData; }
  85.             set {
  86.                 if (value != _cityData) {
  87.                     _cityData = value;
  88.                     onPropertyChanged("cityData");
  89.                     GetWeatherData();
  90.                 }
  91.             }
  92.         }
  93.  
  94.         private DailyForecast _currentDay;
  95.         public DailyForecast currentDay {
  96.             get { return _currentDay; }
  97.             set {
  98.                 if (value != _currentDay) {
  99.                     _currentDay = value;
  100.                     onPropertyChanged("currentDay");
  101.                 }
  102.             }
  103.         }
  104.  
  105.         private bool _ring;
  106.         public bool ring {
  107.             get { return _ring; }
  108.             set {
  109.                 if (value != _ring) {
  110.                     _ring = value;
  111.                     onPropertyChanged("ring");
  112.                 }
  113.             }
  114.         }
  115.  
  116.  
  117.  
  118.         public ObservableCollection<DailyForecast> dailyForecasts { get; set; }
  119.  
  120.  
  121.         public WeatherVM() {
  122.             GetCuurentLocation();
  123.             dailyForecasts = new ObservableCollection<DailyForecast>();
  124.             ring = true;
  125.         }
  126.  
  127.         private async void GetCuurentLocation() {
  128.                cityData = await BingLocator.GetCityData();
  129.         }
  130.  
  131.         public async void GetWeatherData() {
  132.        
  133.             var geoposition = await LocationManager.GetGeopositionAsync();
  134.             var currentLocationKey = await WeatherAPI.GetCityDstaAsync(geoposition.Coordinate.Point.Position.Latitude, geoposition.Coordinate.Point.Position.Longitude);
  135.             var weatherData = await WeatherAPI.GetWeatherAsync(currentLocationKey.Key);
  136.             if (weatherData != null) {
  137.  
  138.                 foreach (var item in weatherData.DailyForecasts) {
  139.                     dailyForecasts.Add(item);
  140.                 }
  141.             }
  142.             ring = false;
  143.             currentDay = dailyForecasts[0];
  144.  
  145.         }
  146.         public event PropertyChangedEventHandler PropertyChanged;
  147.  
  148.         private void onPropertyChanged(string property) {
  149.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
  150.         }
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement