我无法在枢轴页面上显示随机生成的数字。以下是我的代码,
<!--LayoutRoot is the root grid where all page content is placed-->
<ScrollViewer>
<Grid x:Name="LayoutRoot" Background="Transparent" Height="768" Width="480">
<!--Pivot Control-->
<phone:Pivot Title="MY APPLICATION" Loaded="Pivot_Loaded">
<!--Pivot item one-->
<phone:PivotItem Header="Stats">
<Border
BorderBrush="{StaticResource PhoneBackgroundBrush}"
BorderThickness="{StaticResource PhoneBorderThickness}" Background="Black">
<Grid Margin="-3,75,-17,-75">
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,121,253,422" FontSize="30" Text="Highest Temp : " />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,0,253,551" Text="Current Temp :" FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,240,253,311" Text="Lowest Temp : " FontSize="30" />
<TextBlock
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="10,360,253,191" Text="Average Temp : " FontSize="30" />
<TextBlock Name="TempAverage"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,360,41,191" Text="-" FontSize="30" />
<TextBlock Name="TempLowest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,240,41,311" Text="-" FontSize="30" />
<TextBlock Name="TempHighest"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,121,41,430" Text="-" FontSize="30" />
<TextBlock Name="TempCurrent"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="222,0,41,551" Text="-" FontSize="30" />
</Grid>
</Border>
</phone:PivotItem>
然后它告诉我错误&#34;名称&#39; TempAverage在当前上下文中不存在&#39;。与TempCurrent,TempHighest和TempLowest相同。我面临的错误是什么?我应该怎么做呢?
int a = 0;
void time_Tick(object sender, EventArgs e)
{
DispatcherTimer time = new DispatcherTimer();
Random rnd = new Random();
double TempRandom = rnd.Next(100, 500); //generate random number
double TempRandom2 = TempRandom / 10;
***TempCurrent.Text = TempRandom2.ToString("0.0");
if(TempRandom2 > Max)
{
Max = TempRandom2; //Highest Temp = Current Temp
TempHighest.Text = Max.ToString("0.0");
}
if(TempRandom2 < Min)
{
Min = TempRandom2;
TempLowest.Text = Min.ToString("0.0"); //Lowest Temp = Current Temp
}
AvgTemp[a++ % 100] = TempRandom2;
if (a > 100)
TempAverage.Text = AvgTemp.Average().ToString("0.0");
else
TempAverage.Text = (AvgTemp.Sum() / a).ToString("0.0");***
}
答案 0 :(得分:0)
您需要为元素添加名称。例如。 `的TextBlocks。为元素添加名称时,可以在代码中引用它们。像这样。
<TextBlock Name="TempCurrent"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
Margin="10,0,253,551"
Text="Current Temp :"
FontSize="30" />