更新屏幕/显示屏上的数据信息

时间:2012-12-01 15:02:51

标签: c# silverlight windows-phone-7

我希望我的应用程序在代码执行时更新屏幕信息。我尝试使用this.LayoutRoot.UpdateLayout();,但它没有用,我不明白为什么。谁能帮我? 应用程序从单击的按钮接收按钮适当性,然后将其用于Model类中的多个内容,然后我希望它向用户显示消息。之后,我希望它继续执行更多的事情(AI)...:S

public void showMsgFromModel(string player, string msg)
    {
        if(player!="")
            txNomeMsg.Text = player + ":";
        else
            txNomeMsg.Text = player;

        txMsg.Text = msg;
        this.LayoutRoot.UpdateLayout();
        System.Threading.Thread.Sleep(1500);
    }

1 个答案:

答案 0 :(得分:0)

您正在尝试从UI线程进行一些处理,因此无法更新界面。

使用后台工作程序执行长时间运行的任务,并在需要更新UI时使用调度程序:

var worker = new BackgroundWorker();

worker.DoWork += (s, e) =>
{
    Thread.Sleep(1500); // Some processing

    Dispatcher.BeginInvoke(() => txMsg.Text = "Hello"); // Update the UI

    Thread.Sleep(1500); // More processing
};

worker.RunWorkerAsync();