Advertisement
eduardogr

Untitled

Oct 9th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 KB | None | 0 0
  1.  Model
  2.  
  3.  public class Main: Notify {
  4.         private double _temp;
  5.         public double temp {
  6.             get { return _temp; }
  7.             set {
  8.                 if (value != _temp) {
  9.                     _temp = value;
  10.                     onPropertyChanged("temp");
  11.                 }
  12.             }
  13.         }
  14.  
  15.         private double _temp_min;
  16.         public double temp_min {
  17.             get { return _temp_min; }
  18.             set {
  19.                 if (value != _temp_min) {
  20.                     _temp_min = value;
  21.                     onPropertyChanged("temp_min");
  22.                 }
  23.             }
  24.         }
  25.  
  26.         private double _temp_max;
  27.         public double temp_max {
  28.             get { return _temp_max; }
  29.             set {
  30.                 if (value != _temp_max) {
  31.                     _temp_max = value;
  32.                     onPropertyChanged("temp_max");
  33.                 }
  34.             }
  35.         }
  36.     }
  37.  
  38.     public class Weather: Notify {
  39.         private string _main;
  40.         public string main {
  41.             get { return _main; }
  42.             set {
  43.                 if (value != _main) {
  44.                     _main = value;
  45.                     onPropertyChanged("main");
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private string _description;
  51.         public string description {
  52.             get { return _description; }
  53.             set {
  54.                 if (value != _description) {
  55.                     _description = value;
  56.                     onPropertyChanged("description");
  57.                 }
  58.             }
  59.         }
  60.  
  61.         private string _icon;
  62.         public string icon {
  63.             get { return _icon; }
  64.             set {
  65.                 if (value != _icon) {
  66.                     _icon = value;
  67.                     onPropertyChanged("icon");
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.  
  74.     public class Day: Notify {
  75.         private Main _main;
  76.         public Main main {
  77.             get { return _main; }
  78.             set {
  79.                 if (value != _main) {
  80.                     _main = value;
  81.                     onPropertyChanged("main");
  82.                 }
  83.             }
  84.         }
  85.  
  86.         private List<Weather> _weather;
  87.         public List<Weather> weather {
  88.             get { return _weather; }
  89.             set {
  90.                 if (value != _weather) {
  91.                     _weather = value;
  92.                     onPropertyChanged("weather");
  93.                 }
  94.             }
  95.         }
  96.  
  97.         private string _dt_txt;
  98.         public string dt_txt {
  99.             get { return _dt_txt; }
  100.             set {
  101.                 if (value != _dt_txt) {
  102.                     _dt_txt = value;
  103.                     onPropertyChanged("dt_txt");
  104.                 }
  105.             }
  106.         }
  107.  
  108.  
  109.     }
  110.  
  111.     public class RootObject: Notify {
  112.         private List<Day> _list;
  113.         public List<Day> list {
  114.             get { return _list; }
  115.             set {
  116.                 if (value != _list) {
  117.                     _list = value;
  118.                     onPropertyChanged("list");
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
  124.  
  125. LocationManager
  126.  
  127.   public class LocationManager {
  128.  
  129.         public static async Task<Geoposition> GetGeopositionAsync() {
  130.  
  131.             var status = await Geolocator.RequestAccessAsync();
  132.  
  133.             if (status == GeolocationAccessStatus.Denied) {
  134.  
  135.                 throw new Exception();
  136.             }
  137.  
  138.             var locator = new Geolocator { DesiredAccuracy = PositionAccuracy.High };
  139.  
  140.             var position = await locator.GetGeopositionAsync();
  141.  
  142.             return position;
  143.         }
  144.     }
  145.  
  146. MapLocator
  147.  
  148.   const string KEY = "UNtijPSBDvr0h0U68vUX~khL3P_FpMNw2Ik_CpvHUUQ~Aszrntk4oWtZE6ZxGzDK34kutoDZy_-rBh4x2hAd2oKnL7rTF6bckQ06wjDIO9hu";
  149.  
  150.         public static async Task<string> GetCityData() {
  151.  
  152.             var citydata = await LocationManager.GetGeopositionAsync();
  153.  
  154.             BasicGeoposition geoposition = new BasicGeoposition() {
  155.                 Latitude = citydata.Coordinate.Point.Position.Latitude, Longitude = citydata.Coordinate.Point.Position.Longitude
  156.             };
  157.             Geopoint geopoint = new Geopoint(geoposition);
  158.  
  159.             MapService.ServiceToken = KEY;
  160.  
  161.             MapLocationFinderResult finderResult = await MapLocationFinder.FindLocationsAtAsync(geopoint);
  162.  
  163.             if (finderResult.Status != MapLocationFinderStatus.Success) {
  164.                 return null;
  165.             }
  166.  
  167.             return $"{finderResult.Locations[0].Address.Town},{finderResult.Locations[0].Address.CountryCode}";
  168.         }
  169.  
  170. OpenWeatheApi
  171.  
  172.     public class WeatherAPI {
  173.  
  174.         const string KEY = "d3b450256f7cf6125f8f26eef24a5966";
  175.         const string URL = "https://api.openweathermap.org/data/2.5/forecast?q={0},{1}&appid={2}&units=metric";
  176.  
  177.  
  178.         public static async Task<RootObject> GetWeatherDataAsync(string city, string countryCode) {
  179.  
  180.             var result = new RootObject();
  181.  
  182.             var apiURL = string.Format(URL, city, countryCode, KEY);
  183.  
  184.             using (var client = new HttpClient()) {
  185.  
  186.                 var request = await client.GetAsync(apiURL);
  187.                 var response = await request.Content.ReadAsStringAsync();
  188.  
  189.                 result = JsonConvert.DeserializeObject<RootObject>(response);
  190.  
  191.                 return result;
  192.  
  193. XAML
  194.  
  195.    <Page.Resources>
  196.         <vm:WeatherVM x:Key="vm" />
  197.     </Page.Resources>
  198.  
  199.     <RelativePanel DataContext="{StaticResource vm}">
  200.         <AutoSuggestBox Margin="30"
  201.                         x:Name="QueryBox"
  202.                         Width="300"
  203.                         QueryIcon="Find"
  204.                         PlaceholderText="Plase wait"
  205.                         Text="{Binding cityData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
  206.         <ListView RelativePanel.Below="QueryBox"
  207.                   Margin="20" ItemsSource="{Binding rootObject.list}">
  208.             <ListView.ItemTemplate>
  209.                 <DataTemplate>
  210.                     <RelativePanel>
  211.                         <TextBlock x:Name="dateTB"
  212.                                    Text="{Binding dt_txt}"
  213.                                    RelativePanel.RightOf="iconTB"
  214.                                    RelativePanel.AlignTopWith="iconTB" />
  215.                         <TextBlock x:Name="highTB"
  216.                                    Text="{Binding main.temp_max}"
  217.                                    RelativePanel.RightOf="iconTB"
  218.                                    RelativePanel.Below="dateTB"
  219.                                    FontSize="10" />
  220.                         <TextBlock x:Name="lowTB"
  221.                                    RelativePanel.RightOf="highTB"
  222.                                    RelativePanel.Below="dateTB"
  223.                                    FontSize="10"
  224.                                    Text="{Binding main.temp_min}"
  225.                                    Margin="10,0,0,0" />
  226.                         <Image x:Name="iconTB"
  227.                                Source="{}"
  228.                                Height="30"
  229.                                Width="30"
  230.                               />
  231.                     </RelativePanel>
  232.                 </DataTemplate>
  233.             </ListView.ItemTemplate>
  234.  
  235.         </ListView>
  236.     </RelativePanel>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement