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 ...
随机推荐
- 【C++竞赛 G】Lines
Time Limit: 3s Memory Limit: 64MB 问题描述 Ljr has several lines. The lines are covered on the X axis. L ...
- 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(6)动态内存分配
一.传统数组的缺点: 1.数组的长度必须事先定制,且只能是常整数,不能是变量 int len = 5; int a[len]; //error 2.传统形式定义的数组,该程序的内存程序员无法手动释放 ...
- PHP移动互联网开发笔记(5)——基础函数库
一.数学函数库 ● floor 舍一取整(向下取整) float floor (float $value); <?php echo(floor(0.60)."<br>&qu ...
- 最好用的中文速查表(Bash,Gdb,VIM,Nano)
最好用的中文速查表(Cheatsheet) 当年学习 Linux 时就是靠着一张常用命令小卡片,敲啥命令忘记了,经常拿起来看看,后来知道这玩意儿叫做速查表(Cheatsheet),于是开始有意识收集和 ...
- 【BZOJ 1037】[ZJOI2008]生日聚会Party
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1037 [题意] [题解] /* 设f[i][j][k][l] 表示前i个人中,有j个男 ...
- 【26.09%】【codeforces 579C】A Problem about Polyline
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 云主机启动Node服务后,关闭控制台,无法访问的问题
之前一直用node app.js操作,开启服务后,关闭控制台,仍然可以正常访问我的网站.但昨晚新买腾讯云的服务器后,发现关闭控制台后,就无法访问网站了.然后给腾讯云发了个工单.腾讯云的工程师给了一篇技 ...
- Java入门程序
JavaC.exe 编译器,编译.java文件 Java.exe 解释器,执行class文件 编译命令 javac HelloWorld.java 编译后 会产生同名的.class文件 javac编 ...
- C#MVC中创建多模块web应用程序
当一个应用程序有越来越多的子模块后,应用程序将变得越来越大,复杂度也越来越高,应用程序也越来越难维护.如果把每个子模块,独立分成不同的web应用程序,则这个项目将易于维护.关于这个的好处,我也描述得不 ...
- 【20.19%】【codeforces 629D】Babaei and Birthday Cake
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...