取消订阅受保护的覆盖空隙中的事件处理程序

时间:2013-02-26 10:12:20

标签: c# wpf

是否可以从受保护的覆盖void取消订阅事件处理程序?

 protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
    {
        base.OnViewLoaded(sender, e);
        list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
        scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);       
    }

  void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {  
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer sb = e.OriginalSource as ScrollViewer;
        if (sb.ContentVerticalOffset==sb.ScrollableHeight)
        {
           scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
        }
    }

我的问题是我无法访问scroll_ScrollChanged方法中的滚动对象。

2 个答案:

答案 0 :(得分:0)

发件人应该是您需要的对象的引用。

protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
    {
        base.OnViewLoaded(sender, e);
        list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
        scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);       
    }

  void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {  
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer sb = sender as ScrollViewer;
        if (sb.ContentVerticalOffset==sb.ScrollableHeight)
        {
           scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
        }
    }

答案 1 :(得分:0)

此代码未经过测试,但您无法简单地将发件人对象强制转换为ScrollViewer并取消订阅此类事件:

protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
    {
        base.OnViewLoaded(sender, e);
        list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
        scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);       
    }

  void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {  
        ConfigureAndSuperviseInputControls(this.list);
        ScrollViewer scroll = (ScrollViewer)sender;
        if (scroll.ContentVerticalOffset==scroll.ScrollableHeight)
        {
           scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
        }
    }