Wp7 and ListBox rolling data loading

Source: Internet
Author: User

 

I have found articles about ListBox rolling data loading, but now I can't find those articles. However, accidentally find a better post on ListBox rolling loading the data http://social.msdn.microsoft.com/Forums/zh-CN/windowsphonezhchs/thread/4b9ebac8-e1c1-4522-9f11-5e0567f95182 to find the Handler:

The main purpose of this example is to implement ListBox rolling data loading. Suppose that the default value is 30 data records. When it is rolled to the bottom, 20 data records are loaded again, and so on.
The problem to be solved: How do I determine to scroll to the bottom and process requests for loading data?

The idea is as follows:
1. Check the status of the ScrollViewer control in ListBox.
2. If the status is not rolling, determine whether to load data based on the ExtentHeight and VerticalOffset of ScrollViewer and execute the request.

1. Add view status detection code

First, add an empty ListBox in XAML, as shown below:

After adding the XAML, you can check the code that changes the status of ScrollViewer In The ListBox in the background code. In this step, another two methods are required:

1. Find the element code in the specified object view tree based on The View tree:

2. Find the view status in the element based on the given element and status name. The Code is as follows:


After these two methods are available, create a method to add data to ListBox. The Code is as follows:

Then, the preparation is complete. Register the page Load event below and use the above method in the event to achieve our goal. The Code is as follows:

Ii. How to Determine the rolling position of ListBox

A key step has been implemented in the Code, namely capturing the ScrollViewer view status change event contained in ListBox. The problem that needs to be solved below is how to determine whether to scroll to the bottom and whether to scroll to add data. To solve this problem, you need to know several attributes of ScrollViewer:

1. ExtentHeight gets the vertical size of all content displayed in ScrollViewer. (Total)
2. VerticalOffset gets a value that contains the vertical offset of the scrolling content. (Scroll to the nth number)
3. ViewportHeight gets a value that contains the vertical size of visible content. (Several items can be displayed on the page)

If you know the meanings of these values, you can calculate the current rolling position:

By default, 30 pieces of data are added to the ListBox, and the running effect is shown in the following figure:

Initial effect:

The following is the rolling process:

As you can see, ExtentHeight = 30, the current VerticalOffset = 10.5. It does not scroll to the bottom.
Continue rolling.

At this time, the code for adding an Item has been rolled to the bottom and executed.ExtentHeight = 50.

3. End

The ListBox dynamic data loading is complete. So what are the problems with this implementation?
1. If the data comes from the server, there is asynchronous latency and time-consuming operations will affect the UI thread?
2. Avoid rolling requests to the end multiple times during asynchronous loading.

Reference connection
----------------------------------------------------------------
1.Windows Phone Mango change, Listbox: How to detect compression (end of scroll) states?
2.How to detect when a list is scrolling (or not)
 

A. Previously I found the following method for loading data by rolling The listBox (the comment below is what I guess the original author meant ):

Private childItem FindVisualChild <childItem> (DependencyObject obj) where childItem: DependencyObject

{

For (int I = 0; I <VisualTreeHelper. GetChildrenCount (obj); I ++)

{
DependencyObject child = VisualTreeHelper. GetChild (obj, I); // find the child element under the obj view tree
If (child! = Null & child is childItem) // searches for the given child element type childItem. If the child element meets the condition, the return value is returned.
Return (childItem) child;
If else // does not match, repeat the FindVisualChild function <childItem> (child); to find until the child element type specified in the matching function is found.
{
ChildItem childOfChild = FindVisualChild <childItem> (child );
If (childOfChild! = Null)
Return childOfChild;
}

}
}

Problems: 1) the function looks for the child element of obj from the very beginning, and then uses this child element to find the matched child element type. For controls like scrollViewer, it looks for scrollviewer, the result will always be null; 2) and once if... Else ..., ChildItem childOfChild = FindVisualChild <childItem> (child); continues searching once in a loop. If no result is found, the final value is null. It is useless to write the for loop here.

B. This article provides a good method:

Private childItem FindVisualChild <childItem> (DependencyObject obj) where childItem: DependencyObject

{

While (obj! = Null)
{
If (obj is childItem)
Return obj as childItem;

Obj = VisualTreeHelper. GetChild (obj, 0 );
}
Return null;

}

It solves the problems caused by. 1) directly start searching for matched child element types, and the direct child element of some controls is the child element type to be searched. If the child element in the first layer cannot be found, VisualTreeHelper. GetChild (obj, 0); obtain the child element in the next layer, and then continue searching for the matched child element type; 2) nullwhile (obj! = Null) indicates that this function basically does not return null and will always match the search until it is found.

 

Operations are generated when the listBox is pulled to the end or when a part is pulled, which is determined based on different requirements. It is determined based on the rolling status StateGroup group = MainBean. FindVisualState (element, "ScrollStates"); and group. CurrentStateChanged events of listBoxVisual. ScrollViewer. VerticalOffset obtains the height pulled by the scroll bar. ScrollViewer. VerticalOffset> = scrollViewer. ScrollableHeight (scroll to the end, scroll bar> actual scrollable height );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.