コード例 #1
0
        internal static UnitsFlags ParseUnitsFlags(List <String> Args)
        {
            UnitsFlags    Result      = UnitsFlags.Units;
            List <String> ToBeRemoved = new List <String>();

            foreach (String Arg in Args)
            {
                switch (Arg.ToUpper())
                {
                case "--HELP":
                    Result |= UnitsFlags.Units;
                    break;

                case "--TABLE":
                    Result |= UnitsFlags.Table;
                    break;

                default:
                    break;
                }
                if (Arg.StartsWith("--"))
                {
                    ToBeRemoved.Add(Arg);
                }
            }
            foreach (String Arg in ToBeRemoved)
            {
                Args.Remove(Arg);
            }
            return(Result);
        }
コード例 #2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.Inflate(Resource.Layout.Weather, container, false);

            temperature        = v.FindViewById <TextView>(Resource.Id.temperature);
            minimumTemperature = v.FindViewById <TextView>(Resource.Id.minimumtemperature);
            maximumTemperature = v.FindViewById <TextView>(Resource.Id.maximumtemperature);
            city     = v.FindViewById <TextView>(Resource.Id.city);
            humidity = v.FindViewById <TextView>(Resource.Id.humidity);
            TelephonyManager tm          = (TelephonyManager)Activity.GetSystemService(Context.TelephonyService);
            string           countryCode = tm.NetworkCountryIso;

            string     thecity           = configurationManager.RetrieveAValue(ConfigurationParameters.City, "");
            UnitsFlags unitsFlags        = UnitsFlags.Metric;
            string     temperatureSuffix = "°C";

            if (configurationManager.RetrieveAValue(ConfigurationParameters.UseImperialSystem) == true)
            {
                unitsFlags        = UnitsFlags.Imperial;
                temperatureSuffix = "°F";
            }

            ThreadPool.QueueUserWorkItem(async m =>
            {
                var weather = await Weather.GetWeather(thecity, countryCode, unitsFlags);
                Activity.RunOnUiThread(() =>
                {
                    temperature.Text        = weather?.MainWeather.Temperature.ToString() + temperatureSuffix;
                    minimumTemperature.Text = "min: " + weather?.MainWeather.MinTemperature.ToString() + temperatureSuffix;
                    maximumTemperature.Text = "max: " + weather?.MainWeather.MaxTemperature.ToString() + temperatureSuffix;
                    city.Text     = weather?.Name + ": " + weather?.Weather[0].Description;
                    humidity.Text = Resources.GetString(Resource.String.humidity) + ": " + weather?.MainWeather.Humidity.ToString();
                });
            });
            return(v);
        }
コード例 #3
0
        //This class will be the one that connects to the api and provide Lockscreen with Weather information.
        public static async Task <WeatherRoot> GetWeather(string city, string country, UnitsFlags unitsformat)
        {
            string units = "";

            switch (unitsformat)
            {
            case UnitsFlags.Metric:
                units = "metric";
                break;

            case UnitsFlags.Imperial:
                units = "imperial";
                break;

            default:
                units = "metric";
                break;
            }

            string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0},{1}&units={2}&appid=9ca11a6f4426446b991ff390d4f7430f", city, country, units);

            using (var client = new HttpClient())
            {
                try
                {
                    var json = await client.GetStringAsync(url);

                    if (string.IsNullOrWhiteSpace(json))
                    {
                        return(null);
                    }

                    WeatherRoot weatherRoot =
                        DeserializeObject <WeatherRoot>(json);
                    return(weatherRoot);
                }
                catch
                {
                    return(null);
                }
            }
        }