我正在编写一个应用程序来计算从 0 到用户指定的数字的所有素数。我的算法工作正常,但似乎 TextBlock 的高度有限:大约在 500 左右,它不会继续写数字。我仍然可以向下滚动,但数字不再出现。XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer>
<StackPanel Name="stack">
<TextBlock>Calculate prime numbers from</TextBlock>
<TextBox Name="from"></TextBox>
<TextBlock>to</TextBlock>
<TextBox Name="to"></TextBox>
<Button Name="Run" Click="run">Calculate prime numbers!</Button>
<TextBlock Name="result">Hallo</TextBlock>
</StackPanel>
</ScrollViewer>
</Grid>
C#:
public void run(object sender, RoutedEventArgs e)
{
int min; int.TryParse(from.Text, out min);
int max; int.TryParse(to.Text, out max);
List<int> numbers = new List<int>();
for (int i = min; i <= max; i++)
{
numbers.Add(i);
}
int checknext = 2;
while (checknext < Math.Sqrt(max))
{
for (int i = numbers.Count - 1; i >= 0; i--)
{
if (!(numbers[i] == checknext))
{
if (numbers[i] % checknext == 0)
numbers.RemoveAt(i);
}
}
checknext++;
}
result.Text = "";
result.MaxHeight = double.MaxValue;
for (int i = 0; i < numbers.Count; i++)
{
result.Text += numbers[i].ToString() + "\n";
}
}