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 = e.OriginalSource as ScrollViewer;
        if (sb.ContentVerticalOffset==sb.ScrollableHeight)
        {
           scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
        }
    }

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

4

2 回答 2

0

Sender 应该是对您需要的对象的引用。

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); 
        }
    }
于 2013-02-26T10:15:32.323 回答
0

此代码未经测试,但您不能简单地将 sender 对象转换为 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); 
        }
    }
于 2013-02-26T10:16:56.380 回答