public class InvokeHelper
{
#region delegates
private delegate object MethodInvoker(Control control, string methodName, params object[] args);
private delegate object PropertyGetInvoker(Control control, object noncontrol, string propertyName);
private delegate void PropertySetInvoker(Control control, object noncontrol, string propertyName, object value);
#endregion #region static methods
// helpers
private static PropertyInfo GetPropertyInfo(Control control, object noncontrol, string propertyName)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
{
PropertyInfo pi = null;
Type t = null; if (noncontrol != null)
t = noncontrol.GetType();
else
t = control.GetType(); pi = t.GetProperty(propertyName); if (pi == null)
throw new InvalidOperationException(
string.Format(
"Can't find property {0} in {1}.",
propertyName,
t.ToString()
)); return pi;
}
else
throw new ArgumentNullException("Invalid argument.");
}
// outlines
public static object Invoke(Control control, string methodName, params object[] args)
{ if (control != null && !string.IsNullOrEmpty(methodName))
if (!control.CheckAccess())
return control.Dispatcher.Invoke(
new MethodInvoker(Invoke),
control,
methodName,
args
);
else
{
MethodInfo mi = null; if (args != null && args.Length > )
{
Type[] types = new Type[args.Length];
for (int i = ; i < args.Length; i++)
{
if (args[i] != null)
types[i] = args[i].GetType();
} mi = control.GetType().GetMethod(methodName, types);
}
else
mi = control.GetType().GetMethod(methodName); // check method info you get
if (mi != null)
return mi.Invoke(control, args);
else
throw new InvalidOperationException("Invalid method.");
}
else
throw new ArgumentNullException("Invalid argument.");
} public static object Get(Control control, string propertyName)
{
return Get(control, null, propertyName);
}
public static object Get(Control control, object noncontrol, string propertyName)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
if (!control.CheckAccess())
return control.Dispatcher.Invoke(new PropertyGetInvoker(Get),
control,
noncontrol,
propertyName
);
else
{
PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);
object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null)
if (pi.CanRead)
return pi.GetValue(invokee, null);
else
throw new FieldAccessException(
string.Format(
"{0}.{1} is a write-only property.",
invokee.GetType().ToString(),
propertyName
)); return null;
}
else
throw new ArgumentNullException("Invalid argument.");
} public static void Set(Control control, string propertyName, object value)
{
Set(control, null, propertyName, value);
}
public static void Set(Control control, object noncontrol, string propertyName, object value)
{
if (control != null && !string.IsNullOrEmpty(propertyName))
if (!control.CheckAccess())
control.Dispatcher.Invoke(new PropertySetInvoker(Set),
control,
noncontrol,
propertyName,
value
);
else
{
PropertyInfo pi = GetPropertyInfo(control, noncontrol, propertyName);
object invokee = (noncontrol == null) ? control : noncontrol; if (pi != null)
if (pi.CanWrite)
pi.SetValue(invokee, value, null);
else
throw new FieldAccessException(
string.Format(
"{0}.{1} is a read-only property.",
invokee.GetType().ToString(),
propertyName
));
}
else
throw new ArgumentNullException("Invalid argument.");
}
#endregion
}

WPF非UI线程获取修改控件属性值的方法的更多相关文章

  1. C#用副线程改主线程(UI线程)的控件属性的方法(包括Winform和WPF)

    C#用副线程去试图修改主线程的UI控件会报出异常,解决方案是使用副线程注册事件通知主线程自己去修改UI控件 在winform中,方法如下 private void button1_Click(obje ...

  2. C# 线程获取/设置控件(TextBox)值

    线程读写控件需要用委托(delegate)与Invoke/BeginInvoke来进行 参考内容:http://www.cnblogs.com/runner/archive/2011/12/30/23 ...

  3. WPF-学习笔记 动态修改控件Margin的值

    原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...

  4. Jquary获取页面控件的值

    一 Jquery获得服务器控件值的方法由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法: 服务器控件代码:<as ...

  5. selenium修改控件属性

    起因:在进行退出的时候,控件属性显示为不可显示,于是在界面上是不可以点击的,如果想点击这个按钮,只能通过修改控件属性,进行点击 上图看控件,正常来说,点击坐边的用户名,下拉会出现一个logout,退出 ...

  6. 将字符串映射为Delphi控件名,批量修改控件属性

    http://blog.sina.com.cn/s/blog_4dfbd07c01000a81.html 将字符串映射为Delphi控件名,批量修改控件属性 (2007-10-08 14:50:51) ...

  7. WPF 非UI线程更新UI界面的各种方法小结

    转载:https://www.cnblogs.com/bdbw2012/articles/3777594.html 我们知道只有UI线程才能更新UI界面,其他线程访问UI控件被认为是非法的.但是我们在 ...

  8. WPF非UI线程访问网络资源造成页面假死现象

    公司内部一个项目是用WPF作为GUI 访问web接口的形式获取数据, 但是由于数据量比较大,也没做分页,于是就需要一个loading的控件,网上查了很多资料但都比较浅.这里完成需求后,总结一下. 首先 ...

  9. CodedUI Test 测试WPF程序,无法获取控件属性值的解决方法

    注意注意!ItemStatus 在VS2010的CUIT里面是没有的!需要2013以上的版本才可使用. 公司新程序使用WPF制作,但使用CodedUI Test进行自动化测试的时候,很多控件抓取不到其 ...

随机推荐

  1. C# 反射类型转换

    /// <summary> /// 泛型类型转换 /// </summary> /// <typeparam name="T">要转换的基础类型 ...

  2. POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

    题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考怎样筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <c ...

  3. Python 存储模型

    1.Python彻底分离了对象和引用,可以认为内存中的对象都是不可修改的,每次修改引用,相当于在堆上重新创建一个对象,引用指向新对象. 2.对于数值和字符串,修改意味着引用指向一个新对象. 3.集合中 ...

  4. jQuery循环滚动新闻列表

    最近由于项目原因,学习了下jquery,实现了一个小小的功能,就是点击公告的上一条下一条来查看滚动条.具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DT ...

  5. hdu 5591 ZYB's Game 博弈论

    ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...

  6. 从来没有天才 靠自己创造未来——Leo鉴书(29)

    之前在网上跟朋友们聊起天才这个话题,我认来从来没什么所谓天才,有朋友认为有的,只是我们定义不同,要不你看看苏轼? 持天才论者持两个观点:有些人天生擅长干某些事儿,也许是基因作怪:有些人的能力是上帝或者 ...

  7. js实现按回车自行提交

    <script type="text/javascript"> document.onkeydown = function (e) { var theEvent = w ...

  8. hdu 4828 Grids(拓展欧几里得+卡特兰数)

    题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...

  9. LINUX内核笔记

    http://blog.chinaunix.net/uid/27119491/list/1.html?cid=161103

  10. 对lua继承中self.__index = self的释疑

    首先看看从lua表中查找一个键时的流程: -- 当从表t中查找键k时,lua处理如下: -- 1.t中是否有k,有则直接返回值,否则第2步 -- 2.t是否有元表, 无则返回nil, 有则第3步 -- ...