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 ...
随机推荐
- [SCSS] Use Standard Built-in SCSS Functions for Common Operations
We can use javascript for color and opacity variations, math, list and map logic or to see if someth ...
- Kinect舒适区范围--UE4 的Blueprint测试范例
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/44748475 作者:ca ...
- 从多路搜索树到 B-树
1. 什么是 B 树 B 树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡二叉树: B 树类似于红黑树,但它们在降低磁盘 I/O 操作数方面要更好一点, 许多数据库系统使用 B 树或者 B 树的 ...
- [React Router v4] Use the React Router v4 Link Component for Navigation Between Routes
If you’ve created several Routes within your application, you will also want to be able to navigate ...
- Android开发:使用ViewDragHelper实现抽屉拉伸效果
事实上,有非常多方法能够实现一个Layout的抽屉拉伸效果,最常常的方法就是自己定义一个ViewGroup,然后控制点击事件.控制移动之类的,这样的方法的代码量多,并且实现起来复杂,后期维护添加其它效 ...
- 通过binlog日志统计dml语句,找出操作频繁的表
mysqlbinlog --no-defaults --base64-output=decode-rows -v -v mysql-bin.000002 |awk '/###/{if($0~/UPDA ...
- 使用Delegate在两个ViewController之间传值
以下就实现了使用Delegate在两个ViewController之间传值,这种场景一般应用在进入子界面输入信息,完后要把输入的信息回传给前一个界面的情况,比如修改用户个人信息,点击修改进入修改界面, ...
- 【b302】侦探推理
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏 ...
- jvm常用优化方案和方法
新生代 GC(Minor GC):指发生在新生代的垃圾收集动作,因为 Java 对象大多都具备朝生夕灭的特性,所以 Minor GC 非常频繁,一般回收速度也比较快. 老年代 GC(Major GC ...
- python 判断一个数为?
1. 判断一个变量是否数字(整数.浮点数)? instance('a', (int, long, float)) True isinstance('a', (int, long, float)) Fa ...