Skip to content

Commit

Permalink
尝试避免某些 System.NullReferenceException
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Oct 27, 2024
1 parent ec1a804 commit 94cf9ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZongziTEK_Blackboard_Sticker.Helpers;
using ZongziTEK_Weather_API;

namespace ZongziTEK_Blackboard_Sticker.Pages
Expand Down Expand Up @@ -46,7 +47,7 @@ public WeatherForecastPage()
{
castWeathers = JsonConvert.DeserializeObject<List<CastWeather>>(File.ReadAllText(castWeatherFilePath));

if (castWeathers.Count == 0)
if (castWeathers == null || castWeathers.Count == 0)
{
UpdateCastWeathers();
}
Expand All @@ -71,14 +72,21 @@ public WeatherForecastPage()
private void UpdateCastWeathers()
{
castWeathers = Weather.GetCastWeathers(MainWindow.Settings.InfoBoard.WeatherCity);
File.WriteAllText(castWeatherFilePath, JsonConvert.SerializeObject(castWeathers, Formatting.Indented));
try
{
File.WriteAllText(castWeatherFilePath, JsonConvert.SerializeObject(castWeathers, Formatting.Indented));
}
catch (Exception ex)
{
LogHelper.NewLog(ex.Message);
}
}

private bool isTodayRainy = false;

private void ShowCastWeathers()
{
if (castWeathers.Count != 0)
if (castWeathers != null && castWeathers.Count != 0)
{
string rainDays = "";
int today = (int)DateTime.Now.DayOfWeek;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using ZongziTEK_Blackboard_Sticker.Helpers;
using ZongziTEK_Weather_API;

namespace ZongziTEK_Blackboard_Sticker.Pages
Expand Down Expand Up @@ -65,15 +66,22 @@ await Task.Run(() =>
{
liveWeather = JsonConvert.DeserializeObject<LiveWeather>(File.ReadAllText(liveWeatherFilePath));

if (liveWeather.adcode != Weather.GetCityCode(MainWindow.Settings.InfoBoard.WeatherCity))
if (liveWeather != null)
{
UpdateLiveWeather();
if (File.Exists("Weather/CastWeather.json"))
if (liveWeather.adcode != Weather.GetCityCode(MainWindow.Settings.InfoBoard.WeatherCity))
{
UpdateLiveWeather();
if (File.Exists("Weather/CastWeather.json"))
{
File.Delete("Weather/CastWeather.json");
}
}
else if (liveWeather.isError)
{
File.Delete("Weather/CastWeather.json");
UpdateLiveWeather();
}
}
else if (liveWeather.isError)
else
{
UpdateLiveWeather();
}
Expand All @@ -95,12 +103,19 @@ await Task.Run(() =>
private void UpdateLiveWeather()
{
liveWeather = Weather.GetLiveWeather(MainWindow.Settings.InfoBoard.WeatherCity);
File.WriteAllText(liveWeatherFilePath, JsonConvert.SerializeObject(liveWeather, Formatting.Indented));
try
{
File.WriteAllText(liveWeatherFilePath, JsonConvert.SerializeObject(liveWeather, Formatting.Indented));
}
catch (Exception ex)
{
LogHelper.NewLog(ex.Message);
}
}

private void ShowLiveWeather()
{
if (!liveWeather.isError)
if (liveWeather != null && !liveWeather.isError)
{
LabelWeatherInfo.Content = liveWeather.weather + " " + liveWeather.temperature + "℃";
LabelCity.Content = liveWeather.province + " " + liveWeather.city;
Expand Down

0 comments on commit 94cf9ac

Please sign in to comment.