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. 解决ArcGIS Android Could not find class 'com.esri.android.map.MapView'问题

    环境win7 64bit sp1,eclipse 4.2.1 ,android API 16,ADT 23.0.2,arcgis android sdk 10.2.4 从arcgis-android- ...

  2. 4.3、Libgdx启动类和配置

    (原文:http://www.libgdx.cn/topic/45/4-3-libgdx%E5%90%AF%E5%8A%A8%E7%B1%BB%E4%B8%8E%E9%85%8D%E7%BD%AE) ...

  3. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  4. hihocoder #1179 : 永恒游戏 暴力

    #1179 : 永恒游戏 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/11 ...

  5. DebugView 调试工具

    软件标签: DebugView调试工具 用debugview,打开debugview,运行你的debug版本程序,可以定位到源文件的某一行.在vc源码中需要输出的地方用 OutputDebugStri ...

  6. 对比两个同类型的泛型集合并返回差异泛型集合 ——两个List<类名>的比较

    1: /// <summary> 2: /// 对比两个同类型的泛型集合并返回差异泛型集合 3: /// </summary> 4: /// <typeparam nam ...

  7. QWT6.0.1+win7下安装说明

    A) 简介 1.QWT是一个基于LGPL版权协议的开源项目, 可生成各种统计图.它为具有技术专业背景的程序提供GUI组件和一组实用类,其目标是以基于2D方式的窗体部件来显示数据, 数据源以数值,数组或 ...

  8. ARM指令系统

    转载自:http://cxb4224.blog.163.com/blog/static/191224151201110220325976/ ARM处理器的工作状态和工作模式 工作状态 ARM处理器一般 ...

  9. 将一副图片编译进uboot

    在uboot显示图片的时候可以将jpg图片作为uboot的一段,在程序中访问该段,实现图片. 图片: logo.jpg ,将其拷贝到common下 修改u-boot.lds,添加".log& ...

  10. IOS 给图片添加水印(文字)

    有时候上传图片要加唯一标识,简单的就是添加一个水印.这里水印我们讲文字,可以是当前系统时间.坐标.地理位置等 原理就是把一个字符串写到图片上,并且字(font)的大小由图片大小控制. 以下是封装好的一 ...