silverlight属性改变事件通知
工作中遇到silverlight本身没有提供的某些属性改变事件,但又需要在属性改变时得到通知,Google搬运stack overflow,原地址
/// Listen for change of the dependency property
public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
{ //Bind to a depedency property
Binding b = new Binding(propertyName) { Source = element };
var prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached"+propertyName,
typeof(object),
typeof(UserControl),
new System.Windows.PropertyMetadata(callback)); element.SetBinding(prop, b);
}
RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
更正:以上方法可能会造成回调方法callback内存泄漏,改为封装一个方法再调用callback
/// <summary>
/// 监听任意依赖属性值改变事件的辅助方法
/// </summary>
/// <param name="element"></param>
/// <param name="propertyName"></param>
/// <param name="callback"></param>
public static void ListenForChange(FrameworkElement element, string propertyName, PropertyChangedCallback callback)
{
var b = new Binding(propertyName) { Source = element };
var prop = DependencyProperty.RegisterAttached("ListenAttached" + propertyName, typeof(object), typeof(FrameworkElement), new PropertyMetadata(new WeakPropertyChangedCallback(callback).PropertyChangedCallback));
element.SetBinding(prop, b);
} /// <summary>
/// 解决ListenForChange导致的内存泄漏:
/// 避免DependencyProperty.RegisterAttached中元数据对PropertyChangedCallback的引用导致的内存泄漏
/// 但会导致WeakPropertyChangedCallback对象本身的泄漏,WeakPropertyChangedCallback对象本身不大,可以忽略
/// 即:以WeakPropertyChangedCallback对象的内存泄漏(很小) >>>===>>> 替代PropertyChangedCallback所在对象的内存泄漏(可能很大)
///
/// TODO:暂时未找到其他的方式, DependencyProperty.RegisterAttached、PropertyMetadata缺少相关的清除方法
/// silverlight缺少BindingOperations.ClearBinding方法
/// </summary>
class WeakPropertyChangedCallback
{
private WeakReference callback;
public WeakPropertyChangedCallback(PropertyChangedCallback callback)
{ this.callback = new WeakReference(callback);
} /// <summary>
/// 转发到callback
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
public void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (callback.IsAlive)
{
var cb = callback.Target as PropertyChangedCallback;
if (cb != null)
cb(d, e);
}
}
}
silverlight属性改变事件通知的更多相关文章
- WPF——数据绑定及属性改变事件
一.首先需要封装一下文本框的属性,并且在实体类中添加一个实体类的属性改变函数 public class User : INotifyPropertyChanged //INotifyPropertyC ...
- CMFCPropertyGridCtrl的属性改变事件代码
//用于区分Prop, 使用SetData, GetData方法 CMFCPropertyGridProperty* pProp1 = new CMFCPropertyGridProperty(str ...
- Silverlight实例教程 - Validation数据验证基础属性和事件(转载)
Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...
- SQL Server 事件通知(Event notifications)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) 事件通知监控DDL(NotifyQue ...
- HTML5 Audio/Video 标签,属性,方法,事件汇总
HTML5 Audio/Video 标签,属性,方法,事件汇总 (转) 2011-06-28 13:16:48 <audio> 标签属性:src:音乐的URLpreload:预加载au ...
- select change下拉框改变事件 设置选定项,禁用select
select change下拉框改变事件 设置选定项,禁用select 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- iOS:使用代理模式监听开关状态改变事件
记一次解决跨控制器监听开关状态改变的尝试. 为了统一设置UITableViewCell里的内容,自定义了UITableViewCell类的一个基类,命名为SettingCell.SettingCell ...
- combobox 属性、事件、方法
一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...
- HTML5全局属性和事件
全局属性和事件能够应用到所有标签元素上,在HTML4中有许多全局属性,比如id,class等.HTML5中又新增了一些特殊功能的全局属性和事件. 属性: HTML5属性能够赋给标签元素含义和语 ...
随机推荐
- mongodb参数
启动命令 : mongod -port --dbpath data/ --logpath log/mongodb.log --fork ps -ef | grep momgod (查看是否启动成功) ...
- 大白书中无向图的点双联通分量(BCC)模板的分析与理解
对于一个无向图,如果任意两点至少存在两条点不重复(除起点和终点外无公共点)的路径,则这个图就是点双联通. 这个要求等价于任意两条边都存在于一个简单环(即同一个点不能在圈中出现两次)中,即内部无割点. ...
- tomcat设置开机自动启动
windows下: 1 命令cmd 进入 磁盘: 回车 2 tomcat\bin目录 回车 3 service.bat install (注:tomcat一定要有service.bat ...
- 旅游类的APP原型模板分享——Priceline
Priceline是一款旅游服务的APP应用.功能有查找预订酒店.车票.机票等服务. 本原型由国产Mockplus(原型工具)和iDoc(智能标注,一键切图工具)提供. 先简单看看动图: 点击这里,可 ...
- 推荐一款Notepad++主题Dracula
https://draculatheme.com/notepad-plus-plus/ Activating theme Go to %AppData%\Notepad++\themes Place ...
- Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法
原文链接 http://blog.csdn.net/bbdxf/article/details/25594703 [Decode error - output not utf-8]或者[Decode ...
- 2-创建spring boot项目
想要创建一个spring boot项目,最好的方法就是参照官网的例子: https://spring.io/guides/gs/maven/#scratch 创建的过程我就不再啰嗦了,官网描述非常详细 ...
- jquery选择器基础知识(复制w3c)
jQuery 元素选择器 jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") ...
- C++ 使用命名规范
刚开始正式学习C++, 之前写了一个C++ 的小程序,但是并没有注意命名规范之类的.这一次重写一个类似的程序,再加上这几天学习 c++Prime(发现好喜欢这本书.虽然看的很慢,每一小节都感是满满的干 ...
- java 排序算法
1.冒泡排序 public static void main(String[] args) { int[] arr={6,3,8,2,9,1}; System.out.println("排序 ...