WPF 中更新界面信息_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > WPF 中更新界面信息

WPF 中更新界面信息

 2015/4/19 18:26:45  麻将我会  程序员俱乐部  我要评论(0)
  • 摘要:1.Dispatcher.BeginInvokeintii=0;newThread(newParameterizedThreadStart((i)=>{while(true){Thread.Sleep(400);Dispatcher.BeginInvoke(newAction(()=>{ii+=1;btn2.Content=ii.ToString();}));}})).Start();2.动态回调中更新进度条(1中更新界面的方式已经不能直接奏效,需要调用DoEvent(
  • 标签:

 

1.Dispatcher.BeginInvoke

class="brush:csharp;gutter:true;">   int ii = 0;
            new Thread(new ParameterizedThreadStart((i) =>
            {
                while (true)
                {
                    Thread.Sleep(400);
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        ii += 1;
                        btn2.Content = ii.ToString();
                    }));
                }
            })).Start();

2.动态回调中更新进度条(1中更新界面的方式已经不能直接奏效,需要调用DoEvent()方法才能及时更新界面信息)

     private async void Button_Click(object sender, RoutedEventArgs e)
        {
            ServiceAgent.RestFullServiceRequestForFileDownload(new Uri("http://files.cnblogs.com/files/LittleFeiHu/ItcastWP7Simulator20110417.rar"), new DownloadProgressEventHandler((bytes) =>
            {
                S.StudentName = bytes.ToString();
                DoEvents();
                Thread.Sleep(200);
            }));
        }
        private DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
        public void DoEvents()
        {
            DispatcherFrame nestedFrame = new DispatcherFrame();
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
            Dispatcher.PushFrame(nestedFrame);
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
        private static Object ExitFrame(Object state)
        {
            DispatcherFrame frame = state as DispatcherFrame;
            frame.Continue = false;
            return null;
        }

  

 代码下载

  • 相关文章
发表评论
用户名: 匿名