Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Model
- public class Main: Notify {
- private double _temp;
- public double temp {
- get { return _temp; }
- set {
- if (value != _temp) {
- _temp = value;
- onPropertyChanged("temp");
- }
- }
- }
- private double _temp_min;
- public double temp_min {
- get { return _temp_min; }
- set {
- if (value != _temp_min) {
- _temp_min = value;
- onPropertyChanged("temp_min");
- }
- }
- }
- private double _temp_max;
- public double temp_max {
- get { return _temp_max; }
- set {
- if (value != _temp_max) {
- _temp_max = value;
- onPropertyChanged("temp_max");
- }
- }
- }
- }
- public class Weather: Notify {
- private string _main;
- public string main {
- get { return _main; }
- set {
- if (value != _main) {
- _main = value;
- onPropertyChanged("main");
- }
- }
- }
- private string _description;
- public string description {
- get { return _description; }
- set {
- if (value != _description) {
- _description = value;
- onPropertyChanged("description");
- }
- }
- }
- private string _icon;
- public string icon {
- get { return _icon; }
- set {
- if (value != _icon) {
- _icon = value;
- onPropertyChanged("icon");
- }
- }
- }
- }
- public class Day: Notify {
- private Main _main;
- public Main main {
- get { return _main; }
- set {
- if (value != _main) {
- _main = value;
- onPropertyChanged("main");
- }
- }
- }
- private List<Weather> _weather;
- public List<Weather> weather {
- get { return _weather; }
- set {
- if (value != _weather) {
- _weather = value;
- onPropertyChanged("weather");
- }
- }
- }
- private string _dt_txt;
- public string dt_txt {
- get { return _dt_txt; }
- set {
- if (value != _dt_txt) {
- _dt_txt = value;
- onPropertyChanged("dt_txt");
- }
- }
- }
- }
- public class RootObject: Notify {
- private List<Day> _list;
- public List<Day> list {
- get { return _list; }
- set {
- if (value != _list) {
- _list = value;
- onPropertyChanged("list");
- }
- }
- }
- }
- }
- LocationManager
- public class LocationManager {
- public static async Task<Geoposition> GetGeopositionAsync() {
- var status = await Geolocator.RequestAccessAsync();
- if (status == GeolocationAccessStatus.Denied) {
- throw new Exception();
- }
- var locator = new Geolocator { DesiredAccuracy = PositionAccuracy.High };
- var position = await locator.GetGeopositionAsync();
- return position;
- }
- }
- MapLocator
- const string KEY = "UNtijPSBDvr0h0U68vUX~khL3P_FpMNw2Ik_CpvHUUQ~Aszrntk4oWtZE6ZxGzDK34kutoDZy_-rBh4x2hAd2oKnL7rTF6bckQ06wjDIO9hu";
- public static async Task<string> GetCityData() {
- var citydata = await LocationManager.GetGeopositionAsync();
- BasicGeoposition geoposition = new BasicGeoposition() {
- Latitude = citydata.Coordinate.Point.Position.Latitude, Longitude = citydata.Coordinate.Point.Position.Longitude
- };
- Geopoint geopoint = new Geopoint(geoposition);
- MapService.ServiceToken = KEY;
- MapLocationFinderResult finderResult = await MapLocationFinder.FindLocationsAtAsync(geopoint);
- if (finderResult.Status != MapLocationFinderStatus.Success) {
- return null;
- }
- return $"{finderResult.Locations[0].Address.Town},{finderResult.Locations[0].Address.CountryCode}";
- }
- OpenWeatheApi
- public class WeatherAPI {
- const string KEY = "d3b450256f7cf6125f8f26eef24a5966";
- const string URL = "https://api.openweathermap.org/data/2.5/forecast?q={0},{1}&appid={2}&units=metric";
- public static async Task<RootObject> GetWeatherDataAsync(string city, string countryCode) {
- var result = new RootObject();
- var apiURL = string.Format(URL, city, countryCode, KEY);
- using (var client = new HttpClient()) {
- var request = await client.GetAsync(apiURL);
- var response = await request.Content.ReadAsStringAsync();
- result = JsonConvert.DeserializeObject<RootObject>(response);
- return result;
- XAML
- <Page.Resources>
- <vm:WeatherVM x:Key="vm" />
- </Page.Resources>
- <RelativePanel DataContext="{StaticResource vm}">
- <AutoSuggestBox Margin="30"
- x:Name="QueryBox"
- Width="300"
- QueryIcon="Find"
- PlaceholderText="Plase wait"
- Text="{Binding cityData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
- <ListView RelativePanel.Below="QueryBox"
- Margin="20" ItemsSource="{Binding rootObject.list}">
- <ListView.ItemTemplate>
- <DataTemplate>
- <RelativePanel>
- <TextBlock x:Name="dateTB"
- Text="{Binding dt_txt}"
- RelativePanel.RightOf="iconTB"
- RelativePanel.AlignTopWith="iconTB" />
- <TextBlock x:Name="highTB"
- Text="{Binding main.temp_max}"
- RelativePanel.RightOf="iconTB"
- RelativePanel.Below="dateTB"
- FontSize="10" />
- <TextBlock x:Name="lowTB"
- RelativePanel.RightOf="highTB"
- RelativePanel.Below="dateTB"
- FontSize="10"
- Text="{Binding main.temp_min}"
- Margin="10,0,0,0" />
- <Image x:Name="iconTB"
- Source="{}"
- Height="30"
- Width="30"
- />
- </RelativePanel>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </RelativePanel>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement