1

如何从 while、foreach、for 等循环中更新我的 Windows Phone 应用程序中的 UI?

4

1 回答 1

5

您是否考虑过为此使用额外的线程?

public MainPage()
        {
            InitializeComponent();
            Thread thread = new Thread(() => ReadFile(/*params*/));
            thread.Start();
        }

        private void ReadFile(/*params*/)
        {   
            while(/*condition*/)
            {
                /* READ FILE */

                //send task to UI thread to add object to list box
                Dispatcher.BeginInvoke(() => listBox1.Items.Add("YOUR OBJECT"));
            }
        }

长期动作发生在非 UI 线程中,是什么使 UI 线程不会被冻结。在每次循环迭代中,非 UI 线程通过 Dispatcher.BeginInvoke 向 UI 线程发送操作以将新对象添加到列表框。

于 2012-09-15T22:24:42.363 回答