【发布时间】:2021-05-09 12:14:49
【问题描述】:
我试图显示可为空的对象。当变量变为 null 以显示 0 时。 我的对象是双重的?当这个对象为空时,他不会显示出来。
如何在这个对象上设置 0 时为 null ?
我的 xaml 代码:
Label x:Name="SnowForecastFirstDay"
HorizontalTextAlignment="Center"
Grid.Row="2"
Grid.Column="4"
FontSize="15"
TextColor="White"
FontAttributes="Bold"/>
我的 C# 代码:
double? snowForecastFirstDay = weatherBindingData.WeatherDataForecastHourly.List[0].SnowForecast.SnowForecastValue;
if (snowForecastFirstDay == null)
{
SnowForecastFirstDay.Text = "SNOW: 0mm";
}
else
{
SnowForecastFirstDay.Text = "SNOW:" + weatherBindingData.WeatherDataForecastHourly.List[0].SnowForecast.SnowForecastValue.ToString() + "mm";
}
我尝试使用此 if/else 语句,但我的标签未显示在屏幕上。 具有空值的正确方式对象如何出现在显示屏上?
【问题讨论】:
-
使用
snowForecastFirstDay.HasValue判断一个可为空的类型是否有值 -
你能举个例子吗?
-
if (snowForecastFirstDay.HasValue) -
不工作;(我尝试按照你的方式,但标签没有显示
-
您实际上是从哪里获得这些数据的?你确定它真的有价值吗?