我想要做的是我的tic tac toe主菜单的实时背景,它应该在任何按钮中随机放置X和Os所发生的事情就是程序刚刚冻结,当我尝试跟踪它时,调度程序中的代码永远不会得到执行
public partial class MainPage : PhoneApplicationPage
{
Random random;
private Button[] bts;
private int counter;
public MainPage()
{
InitializeComponent();
counter = 0;
bts = new[] { _1, _2, _3, _4, _5, _6, _7, _8, _9 };
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.Relative));
}
private void About_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page4.xaml", UriKind.Relative));
}
private void form_Loaded(object sender, RoutedEventArgs e)
{
random = new Random();
while (true)
{
Dispatcher.BeginInvoke(()=>
{
bts[random.Next() % 9].Content = (counter % 2 == 0) ? "O" : "X";
counter++;
});
}
}
}
答案 0 :(得分:0)
while (true) Dispatcher.BeginInvoke(...);
这是一个无限循环;它会永远冻结你的程序。 (实际上,直到调度程序队列中的内存不足为止)
不要那样做。