前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >silverlight属性改变事件通知

silverlight属性改变事件通知

作者头像
用户6362579
发布2019-09-29 17:28:38
8720
发布2019-09-29 17:28:38
举报
文章被收录于专栏:小神仙小神仙

工作中遇到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);
                }
            }
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-06-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档