コード例 #1
0
ファイル: ViewModel.cs プロジェクト: Inferis/drash-win8
        private async void VisualizeRain(RainData rainData)
        {
            if (Model.RainWasUpdated)
                Animatable.Mode = AnimatableMode.Forced;
            Model.RainWasUpdated = false;

            string chanceText;
            Color chanceColor;
            string mmImage;
            string mmText;

            var chance = rainData != null ? rainData.ChanceForEntries(Model.Entries) : -1;
            if (chance >= 0) {
                chanceText = string.Format("{0}%", chance);
                chanceColor = Colors.White;
            }
            else {
                chanceText = "?";
                chanceColor = Colors.DarkGray;
            }

            var intensity = 0;
            var mm = 0.0;
            if (rainData != null) {
                mm = rainData.PrecipitationForEntries(Model.Entries);
                intensity = rainData.IntensityForEntries(Model.Entries);
            }

            if (intensity > 0 || mm > 0) {
                mm = Math.Max(mm, 0.001);
                intensity = ((int)Math.Max(1, Math.Min(1 + intensity / 25.0, 4)));

                var format = mm < 0.01 ? "{0:0.000}" : "{0:0.00}";
                mmText = Math.Floor(mm) == mm ? string.Format("{0}", (int)mm) : string.Format(format, mm);
                mmImage = intensity.ToString(CultureInfo.InvariantCulture);
            }
            else {
                mmText = "0";
                mmImage = "0";
            }
            mmText = mmText + "\nmm";

            string night;
            if (intensity == 0 && Model.Location != null) {
                var solarinfo = SolarInfo.ForDate(Model.Location.Latitude, Model.Location.Longitude, DateTime.Now);
                var sunrisen = solarinfo.Sunrise < DateTime.UtcNow && DateTime.UtcNow < solarinfo.Sunset;
                night = !sunrisen ? "n" : "d";
            }
            else {
                night = "";
            }
            mmImage = string.Format("ms-appx:/Assets/intensity{0}{1}.png", mmImage, night);

            // we have to jump through silly hoops to have the text change
            if (Animatable.Mode == AnimatableMode.Forced) {
                Animatable.Mode = AnimatableMode.Disabled;
                if (Chance == chanceText) Chance = null;
                if (Precipitation == mmText) Precipitation = null;
                Animatable.Mode = AnimatableMode.Forced;
            }
            Chance = chanceText;
            Precipitation = mmText;

            IntensityImage = new BitmapImage(new Uri(mmImage));

            await Task.Delay(500);
            Animatable.Mode = AnimatableMode.Enabled;
        }