ScrollViewer滚动究竟来触发载入数据的Behavior
生活随笔
收集整理的這篇文章主要介紹了
ScrollViewer滚动究竟来触发载入数据的Behavior
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
近期項目中遇到載入數(shù)據(jù)的性能問題, 原因是.net4.0的虛擬化支持不夠完畢,有好多bug在4.5才修復(fù)。 我們僅僅能利用大家通用的做法來延遲載入數(shù)據(jù):每次載入固定少量的數(shù)據(jù)。當(dāng)拖動究竟后。繼續(xù)載入興許同樣數(shù)量的數(shù)據(jù)。
思路:監(jiān)聽ScrollViewer的VerticalOffsetProperty,假設(shè)值達到同意滾動的高度ScrollableHeight,則發(fā)出event通知外部處理載入邏輯。
用法:1.對于ItemsControl編輯控件模板,在當(dāng)中的ScrollViewer中增加behavior。<ScrollViewer?x?:Name="DG_ScrollViewer"?Focusable="false">?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<i:?Interaction.Behaviors>?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<local:?ReachingBottomBehavior?ReachingBottomEvent="LazyLoadingBehavior_OnReachingBottomEvent" />?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????</i:?Interaction.Behaviors>
在blend中,比較簡單。直接對控件右鍵。編輯模板/副本。進入ScrollViewer后,把這個behavior拖到ScrollViewer的元素下就可以, 生成的代碼就是上面的內(nèi)容。xmlns:i?="http://schemas.microsoft.com/expression/2010/interactivity"
2.實現(xiàn)上面ReachingBottomEvent的處理邏輯。比方:??private?void?LazyLoadingBehavior_OnReachingBottomEvent?()??????? {????????????var?vm?=?LayoutRoot.?DataContext?as?MainViewModel;????????????if?(vm?!=?null)??????????? {????????????????int?count?=?dtgRoot.?Items?.Count?;????????????????for?(int?i?=?count?;?i?<?count?+ 30;?i?++)??????????????? {????????????????????vm?.DataItems?.?Add(?????????????????????????????????????new?DataItem?()???????????????????????????????????? {?????????????????????????????????????????Item1?=?i?.?ToString(),?????????????????????????????????????????Item2?=?i?.?ToString() +?i?.ToString?()???????????????????????????????????? });??????????????? }??????????? }??????? }
代碼例如以下:
?public?class?ReachingBottomBehavior?:?Behavior<?ScrollViewer>??? {????????public?ReachingBottomBehavior?()??????? {????????????// Insert code required on object creation below this point.??????? }
????????protected?override?void?OnAttached()??????? {????????????base.OnAttached?();
????????????// Insert code that you would want run when the Behavior is attached to an object.????????????var?dpd?=?DependencyPropertyDescriptor.?FromProperty(ScrollViewer?.VerticalOffsetProperty,?AssociatedType);????????????dpd.AddValueChanged?(????????????????????????????????AssociatedObject,??????????????????????????????? (sender,???????????????????????????????? args) =>??????????????????????????????? {????????????????????????????????????RaiseReachingBottomEvent();??????????????????????????????? });??????? }
????????protected?override?void?OnDetaching()??????? {????????????base.OnDetaching?();
????????????// Insert code that you would want run when the Behavior is removed from an object.??????? }
????????private?void?RaiseReachingBottomEvent()??????? {????????????bool?isReachingBottom?=?AssociatedObject.?VerticalOffset?>=?AssociatedObject?.ScrollableHeight;????????????if?(isReachingBottom?)??????????? {????????????????if?(this?.ReachingBottomEvent?!=?null)??????????????? {????????????????????this.ReachingBottomEvent?();??????????????? }??????????? }??????? }
????????public?event?Action?ReachingBottomEvent;??? }
思路:監(jiān)聽ScrollViewer的VerticalOffsetProperty,假設(shè)值達到同意滾動的高度ScrollableHeight,則發(fā)出event通知外部處理載入邏輯。
用法:1.對于ItemsControl編輯控件模板,在當(dāng)中的ScrollViewer中增加behavior。<ScrollViewer?x?:Name="DG_ScrollViewer"?Focusable="false">?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<i:?Interaction.Behaviors>?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<local:?ReachingBottomBehavior?ReachingBottomEvent="LazyLoadingBehavior_OnReachingBottomEvent" />?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????</i:?Interaction.Behaviors>
在blend中,比較簡單。直接對控件右鍵。編輯模板/副本。進入ScrollViewer后,把這個behavior拖到ScrollViewer的元素下就可以, 生成的代碼就是上面的內(nèi)容。xmlns:i?="http://schemas.microsoft.com/expression/2010/interactivity"
2.實現(xiàn)上面ReachingBottomEvent的處理邏輯。比方:??private?void?LazyLoadingBehavior_OnReachingBottomEvent?()??????? {????????????var?vm?=?LayoutRoot.?DataContext?as?MainViewModel;????????????if?(vm?!=?null)??????????? {????????????????int?count?=?dtgRoot.?Items?.Count?;????????????????for?(int?i?=?count?;?i?<?count?+ 30;?i?++)??????????????? {????????????????????vm?.DataItems?.?Add(?????????????????????????????????????new?DataItem?()???????????????????????????????????? {?????????????????????????????????????????Item1?=?i?.?ToString(),?????????????????????????????????????????Item2?=?i?.?ToString() +?i?.ToString?()???????????????????????????????????? });??????????????? }??????????? }??????? }
代碼例如以下:
?public?class?ReachingBottomBehavior?:?Behavior<?ScrollViewer>??? {????????public?ReachingBottomBehavior?()??????? {????????????// Insert code required on object creation below this point.??????? }
????????protected?override?void?OnAttached()??????? {????????????base.OnAttached?();
????????????// Insert code that you would want run when the Behavior is attached to an object.????????????var?dpd?=?DependencyPropertyDescriptor.?FromProperty(ScrollViewer?.VerticalOffsetProperty,?AssociatedType);????????????dpd.AddValueChanged?(????????????????????????????????AssociatedObject,??????????????????????????????? (sender,???????????????????????????????? args) =>??????????????????????????????? {????????????????????????????????????RaiseReachingBottomEvent();??????????????????????????????? });??????? }
????????protected?override?void?OnDetaching()??????? {????????????base.OnDetaching?();
????????????// Insert code that you would want run when the Behavior is removed from an object.??????? }
????????private?void?RaiseReachingBottomEvent()??????? {????????????bool?isReachingBottom?=?AssociatedObject.?VerticalOffset?>=?AssociatedObject?.ScrollableHeight;????????????if?(isReachingBottom?)??????????? {????????????????if?(this?.ReachingBottomEvent?!=?null)??????????????? {????????????????????this.ReachingBottomEvent?();??????????????? }??????????? }??????? }
????????public?event?Action?ReachingBottomEvent;??? }
總結(jié)
以上是生活随笔為你收集整理的ScrollViewer滚动究竟来触发载入数据的Behavior的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在SpringMVC中使用Jackson
- 下一篇: AC日记——Power收集 洛谷 P38