WPF刷新界面
Winform 里有 Application.DoEvents();可刷新!
WPF 里没这个,尽管可用委托实现多线程,但是刷新还是不行!
后来找到了 类似App.DoEvents()的方法();
代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
namespace wgscd
{
public partial class App : Application
{
private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
public static 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;
}
}
}
------------------------------------------------------------------
这样在需要调用刷新的地方就可:App.DoEvents();
-----------------例子--------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
namespace wgscd
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
myDothread = new DoThread(doJob);
t.Tick+=new EventHandler(t_Tick);
t.Interval = TimeSpan.FromMilliseconds( 100d);
t.Start();
}
int i = 0;
public delegate void DoThread();
DoThread myDothread;
DispatcherTimer t = new DispatcherTimer();//定义一个 定时器
void t_Tick(object sender ,EventArgs e){
this.Title = DateTime.Now .ToString();
// textBox1.Text = i.ToString();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "11";
myDothread.BeginInvoke(null,null );
}
void doJob() {
i = 0;
//System.Windows.Threading.Dispatcher.Run();
DoThread dotd = delegate()
{
// Thread.Sleep(20000); //这样就会让 按钮(button1)假死,WPF还是 无能为力吗?
while (i < 33773)
{
textBox1.Text =i.ToString();
i++;
// textBox1.UpdateLayout();
App.DoEvents();//这里可刷新文本框!
}
};
this.Dispatcher.BeginInvoke(dotd,null);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "hello";
}
}
}
//--------------------------------------------------------------------------------------------------------
第1种用 Task类. 推荐用这个办法
publicvoid工作_Task()
{
Dispatcher x=Dispatcher.CurrentDispatcher;//取得当前工作线程
//另开线程工作
Task<int>计数=newTask<int>(()=>{return计数方法(); });
计数.ContinueWith(工作完毕后方法);//工作完毕后执行的方法
计数.Start();//开始工作
}
publicvoid工作完毕后方法(Task<int>参数)
{
if(参数.IsCompleted)//正常工作完毕
{
var 结果=参数.Result;//取得结果
//处理结果.
//本方法非界面线程.如果需要在界面线程操作,需要转移到界面线程
}
}
intc;
publicint计数方法()
{
returnc++;
}
第2种方法用线程
publicvoid工作_Thread()
{
Dispatcher x=Dispatcher.CurrentDispatcher;//取得当前工作线程
//另开线程工作
System.Threading.ThreadStart start=delegate()
{
//工作函数
Func<string>fu=newFunc<string>(()=>{return""; });//工作函数
var 工作结果=fu();//开始工作
//异步更新界面
x.BeginInvoke(newAction(()=>
{
//在界面线程操作 可以使用 工作结果
}), DispatcherPriority.Normal);
};
newSystem.Threading.Thread(start).Start();//启动线程
}
第3种方法用 BackgroundWorker.
BackgroundWorker 后台线程;
publicvoid线程初始化()
{
后台线程=newBackgroundWorker();
后台线程.WorkerSupportsCancellation=true;//可以取消
后台线程.DoWork+=newDoWorkEventHandler(后台线程_DoWork);
后台线程.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(后台线程_RunWorkerCompleted);
}
publicvoid启动后台线程()
{
后台线程.RunWorkerAsync();
}
void后台线程_RunWorkerCompleted(objectsender, RunWorkerCompletedEventArgs e)
{
//工作完毕的方法
}
void后台线程_DoWork(objectsender, DoWorkEventArgs e)
{
//工作方法
}
WPF刷新界面的更多相关文章
- WPF刷新界面之坎坷路
WPF刷新界面之坎坷路 项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面. 但是很遗憾,定时器并不能进行刷新.后台检测List数据里面已经添加了很多了很 ...
- 【源码分享】WPF漂亮界面框架实现原理分析及源码分享
1 源码下载 2 OSGi.NET插件应用架构概述 3 漂亮界面框架原理概述 4 漂亮界面框架实现 4.1 主程序 4.2 主程序与插件的通讯 4.2.1 主程序获取插件注册的服务 4.2 ...
- 重复点击主界面(TabBar)按钮刷新界面--点击状态栏回到顶部
1.监听按钮点击 2.判断是否是点击的同一个按钮(记录上次点击的按钮) 3.当重复点击相同按钮时,需要获取当前按钮对应控制器刷新界面 3.1 判断是否重复点击按钮,代码写在哪里? ...
- C#子线程刷新界面并关闭窗体
目的:要循环刷新界面上的控件,同时不影响用户操作.循环结束后关闭窗体. 步骤:先创建一个窗体,窗体中拖入一个lable控件(label1),一个button控件(button1) 代码窗口输入: // ...
- 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。
使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...
- 使用OC和swift创建系统自带的刷新界面
使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用 ...
- ListView中响应item的点击事件并且刷新界面
---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...
- WPF防止界面卡死并显示加载中效果
原文:WPF防止界面卡死并显示加载中效果 网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学 前台: <Window x:Class="WpfApplic ...
- Angular 2/4/5+ 重复点击菜单刷新界面
记一下,网上没找到方法 自己搞了好久 通过跳转到别的界面在跳回来的方式进行实现 //再次点击刷新界面 if (this.router.url == item.ur ...
随机推荐
- 【10.58%】【codeforces 721C】Journey
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Android NDK开发之Jni的数据类型
在前面的一篇博客<Android NDK开发简介>,我简单地说明了Android NDK开发的流程,以及其重要的一环:JNI层得开发.今天我再详细说明一下自己的学习经验. JNI是Java ...
- 基于 MySQL 5.6 keepalived的双主搭建
环境介绍: 说明 IP 节点1 192.168.56.56 节点2 192.168.56.57 w_ip 192.168.56.6 安装keepalived tar -zxvf keepalived- ...
- Navigation Pane不能设置显示标题
https://msdn.microsoft.com/VBA/Word-VBA/articles/view-showheading-method-word https://social.msdn.mi ...
- Android开发和測试实践 - 接入友盟统计
这两年一直在做无线的測试,兴许还会继续去做无线的測试,可是之前由于时间的原因一直都没有非常细致的了解到代码层面. 最近抽出时间自己做了些app的开发,决定假设想把移动的測试做好做深入.有一定的app开 ...
- Hadoop源码分析(MapReduce概论)
大家都熟悉文件系统,在对HDFS进行分析前,我们并没有花非常多的时间去介绍HDFS的背景.毕竟大家对文件系统的还是有一定的理解的,并且也有非常好的文档.在分析Hadoop的MapReduce部分前,我 ...
- 前端Js框架汇总(工具多看)
前端Js框架汇总(工具多看) 一.总结 一句话总结: 二.前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领 ...
- Zepto.js touch,tap增加 touch模块深入分析
1. touch库实现了什么和引入背景 click事件在移动端上会有 300ms 的延迟,同时因为需要 长按 , 双触击 等富交互,所以我们通常都会引入类似 zepto 这样的库.zepto 中tou ...
- win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量
#include <Windows.h> #include <stdio.h> #include <tchar.h> LARGE_INTEGER lgA ...
- Unable to find a single main class from the following candidates
关于start-class,spring boot官方手册是这么说明的: The plugin rewrites your manifest, and in particular it manages ...