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



我希望我的应用程序在执行代码时更新屏幕信息。我尝试了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);
    }

您正在尝试从 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();