2

我正在为 Windows Phone 8 制作 Tic-Tac-Toe 游戏,我希望该游戏与自己进行对战,作为主菜单的背景

private Button[] bts;
private List<Button> temp = new List<Button>();
private int[,] winningConditions;
private int counter;
private string Board;

public MainPage()
{
    InitializeComponent();
    bts = new[] { _1, _2, _3, _4, _5, _6, _7, _8, _9 };
    winningConditions = new[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 },
    { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } };
    counter = 0;
    bTextFont();
}

private void NewGame()
{
    foreach (Button i in bts)
        i.Content = "";//here I get an Exception saying Invalid cross-thread access
    while (true)
    {
        NearlyHuman();
        someOneWon();
        counter++;
    }
}
private void form_Loaded(object sender, RoutedEventArgs e)
{
    Thread backGround = new Thread(new ThreadStart(NewGame));
    backGround.Start();
}
4

2 回答 2

16

您不能直接从任何其他线程访问 UI 线程。因此,在 Dispatcher.BeginInvoke() 中包含您的 UI 访问代码

Dispatcher.BeginInvoke(() =>
    {
        foreach (Button i in bts)
           i.Content = "";
    });
于 2012-11-28T13:06:47.113 回答
0
  • 利用后台工作者

对于Windows Phone 8: http: //msdn.microsoft.com/en-us/library/windows/apps/cc221403 (v=vs.105).aspx

于 2014-11-29T19:50:56.003 回答