C# Windows Phone 8线程无效的跨线程访问

C# Windows Phone 8线程无效的跨线程访问,c#,windows-phone-8,multithreading,C#,Windows Phone 8,Multithreading,我正在为WindowsPhone8制作一个tictac-Toe游戏,我想让游戏以vs本身作为主菜单的背景 private Button[] bts; private List<Button> temp = new List<Button>(); private int[,] winningConditions; private int counter; private string Board; public MainPage() { InitializeCom

我正在为WindowsPhone8制作一个tictac-Toe游戏,我想让游戏以vs本身作为主菜单的背景

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();
}
专用按钮[]基站;
私有列表临时值=新列表();
私有整数[,]赢取条件;
专用int计数器;
私人弦板;
公共主页()
{
初始化组件();
bts=new[]{{u1、{u2、{u3、{u4、{u5、{u6、{u7、{u8、{u9};
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 } };
计数器=0;
bTextFont();
}
私人游戏
{
foreach(基站中的按钮i)
i、 Content=”“;//这里我得到一个异常,表示跨线程访问无效
while(true)
{
几乎是人类的();
有人赢了();
计数器++;
}
}
已加载私有无效表单(对象发送方,路由目标e)
{
线程背景=新线程(新线程开始(新游戏));
backGround.Start();
}

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

  • 使用后台工作人员
对于Windows Phone 8:

非常感谢,但现在可以了,但问题是我想让它成为一个真实的背景,我该怎么做?如果我做了类似于无限循环的事情,程序在完成之前不会对ui做任何更改,这永远不会发生,那么我该怎么做呢
Dispatcher.BeginInvoke(() =>
    {
        foreach (Button i in bts)
           i.Content = "";
    });