123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Threading;
- namespace PackingPress.Common
- {
- public class UIHelper : 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;
- }
- }
- public class UIThreadHelper<T,O> where T : class where O : class
- {
- Window _window;
- Dictionary<string, Func<T,O>> _delegateDic = new Dictionary<string, Func<T,O>>();
- public UIThreadHelper(Window window)
- {
- _window = window;
- }
- public void AddDelegate(string key, Func<T,O> delegateHandle)
- {
- _delegateDic.Add(key, delegateHandle);
- }
- public void AddDelegate(Func<T,O> delegateHandle)
- {
- _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
- }
- public O RunDelegate(string key,params object[] args)
- {
- if (_delegateDic.Keys.Contains(key))
- {
- if (args.Length > 0)
- {
- return _window.Dispatcher.Invoke(_delegateDic[key], args) as O;
- }
- else
- {
- return _window.Dispatcher.Invoke(_delegateDic[key],"") as O;
- }
- }
- return default(O);
- }
- }
- public class UIThreadHelper<T> where T : class
- {
- Window _window;
- Dictionary<string, Action<T>> _delegateDic = new Dictionary<string, Action<T>>();
- public UIThreadHelper(Window window)
- {
- _window = window;
- }
- public void AddDelegate(string key, Action<T> delegateHandle)
- {
- _delegateDic.Add(key, delegateHandle);
- }
- public void AddDelegate(Action<T> delegateHandle)
- {
- _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
- }
- public void RunDelegate(string key, params object[] args)
- {
- if (_delegateDic.Keys.Contains(key))
- {
- if (args.Length > 0)
- {
- _window.Dispatcher.Invoke(_delegateDic[key], args);
- }
- else
- {
- _window.Dispatcher.Invoke(_delegateDic[key], "");
- }
- }
- }
- }
- public class UIThreadHelper
- {
- Window _window;
- Dictionary<string, Action> _delegateDic = new Dictionary<string, Action>();
- public UIThreadHelper(Window window)
- {
- _window = window;
- }
- public void AddDelegate(string key, Action delegateHandle)
- {
- _delegateDic.Add(key, delegateHandle);
- }
- public void AddDelegate(Action delegateHandle)
- {
- _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
- }
- public void RunDelegate(string key, params object[] args)
- {
- if (_delegateDic.Keys.Contains(key))
- {
- _window.Dispatcher.Invoke(_delegateDic[key]);
- }
- }
- }
- }
|