Windows Phone 8.1 Virtualization arranges layout controls

Source: Internet
Author: User

11.2.2 VirtualizingStackPanel, Itemsstackpanel, and Itemswrapgrid virtualization arrange layout controls

VirtualizingStackPanel, Itemsstackpanel, and Itemswrapgrid are all virtualized layout controls, which are rarely used in the layout of the interface, most of which are encapsulated in the layout panel of the list, The main purpose is to virtualize the large amount of data on the list, thus greatly improving the efficiency of the list. In fact, these 3 virtualized layout controls are the default layout arrangement for the list control, where the VirtualizingStackPanel control is the default layout panel for the listbox, and Itemsstackpanel is the default layout panel for the ListView. Itemswrapgrid is the default layout panel for the GridView.

Both the VirtualizingStackPanel control and the Itemsstackpanel control represent the virtualized arrangement of content on one line, either horizontally or vertically. The arrangement layout effects implemented by their controls are the same as the StackPanel controls, and the difference is that the controls can implement the logic of virtualization. For a more data-rich list layout, using the VirtualizingStackPanel control or the Itemsstackpanel control is much more efficient than the StackPanel control, because the virtualization control simply displays the data in the current screen range, Other data is processed by virtualization technology and is not initialized to display the UI, so it is highly efficient. The Itemswrapgrid control implements the grid's virtualized layout effect, and the virtualization principle is similar to the Itemsstackpanel control, except that they are arranged differently.

These virtualized arrangement layout controls calculate the number of visible items and process ItemContainerGenerator from ItemsControl, such as a ListBox, to create UI elements for visible items only. Virtualization occurs only when the item control contained in StackPanel creates its own item container. You can use data binding to ensure that this process occurs, and if you create the item element of the list directly and then add the child object of the layout control to the virtualization arrangement, this is not done in a virtualized manner. Here's a itemsstackpanel to illustrate how we can use virtualization to arrange layout controls to solve some real-world problems.

Itemsstackpanel is the default item host for the ListView element. When you use the ListView list control to bind data, the default is to use the Itemsstackpanel control to arrange the data items. If you use the ItemsControl list control to present the data, to add virtualization functionality to the list, the Itemsstackpanel object element must be included in a itemspaneltemplate. Now let's go back to the first example of this chapter, when you use the ItemsControl control to bind to a collection of 2000 data items, the load is slow, which is the result of not using virtualization. If you use Itemsstackpanel for virtualized layouts on ItemsControl controls, you will find that loading is very fast. To add the Itemsstackpanel virtualization layout to the ItemsControl control, you need to modify the code to read as follows:

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

    <itemscontrol x:name= "ItemsControl" >         <!--Use the Itemsstackpanel control as a layout panel for ItemsControl-->         < ItemsControl.ItemsPanel>             <ItemsPanelTemplate>                 <ItemsStackPanel/>             </ItemsPanelTemplate>        </  itemscontrol.itemspanel>        <itemscontrol.template>             <controltemplate targettype= " ItemsControl ">                 <scrollviewer>                    < itemspresenter/>                 </scrollviewer>            </ controltemplate>        </itemscontrol.template>         <ItemsControl.ItemTemplate>             <DataTemplate>                  Omit several codes             </DataTemplate>         </itemscontrol.itemtemplate>    </ Itemscontrol>

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

If you need to use the Itemsstackpanel control virtualization technology in the list, one thing to keep in mind is that you don't break the structure of ScrollViewer and itemspresenter, otherwise you won't realize the effects of virtualization. If you add a StackPanel control between ScrollViewer and Itemspresenter, as follows:

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

<controltemplate targettype= "ItemsControl" > <ScrollViewer> <StackPanel> <ItemsPresenter/> </StackPanel> </ScrollViewer> </ControlTemplate>

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

At this point, the Itemsstackpanel control will initialize all the data at once, and it will not act as a virtualization. Because the Itemsstackpanel control is virtualized based on the fixed size of each item, the virtualization of the layout is processed, When other controls are added to ScrollViewer and Itemspresenter, the virtualized layout of the Itemsstackpanel control is destroyed, causing the Itemsstackpanel control to not accurately measure the layout of the data items in the list.

11.2.3 Implementing a landscape-oriented virtualization layout

Most of the list layouts we implement are mostly vertical layouts, including the GridView control's layout as a whole. Then the ListView control and the ListBox control are the vertical vertical scrolling list By default, and if you want to let it scroll horizontally then you need to customize the panel of its layout, then we can use the Itemsstackpanel control to implement the If we do not need so many features and effects of the ListView control, we can use the most basic list control ItemsControl control with the Itemsstackpanel control to achieve the effect of horizontal scrolling, and with the function of virtualization.

In this example, we use the ItemsControl control to scroll through the picture horizontally, and in this case we use the horizontal layout of the Itemsstackpanel control. There will be 100 data items in the list, and we can see what the data items are loaded with the log.

Code Listing 11-7 : Landscape Virtualization List (source code: Chapter 11th \examples_11_7)

(1) First, create the entity class and the custom collection class, the entity class item and the custom collection class itemlist.

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

Item.cs File Main code------------------------------------------------------------------------------------------------------------         ------public class Item {//Picture object public BitmapImage image {get; set;}    Picture name public string ImageName {get; set;} }

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

ItemList.cs File Main code-------------------------------------------------------------------------------------------------------- ----------    public class itemlist : ilist    { The number of         //  settings collection is 100         public itemlist ()         {             Count = 100;         }        //  Collection Quantity Properties          public int Count { get; set; }         //  returns data items by index         public object this[int  index]        {             get            {                 //  loading picture is a picture resource inside the program, 5 pictures cyclic loading                  int imageindex  = 5 - index % 5;                 debug.writeline ("Loaded collection index is:"  + index );                 return new item {  ImageName =  "Picture"  + index, image = new bitmapimage (New uri (" Ms-appx:///images/" + imageIndex + ". jpg ",  urikind.relativeorabsolute)  };             }             set            {                 throw new  NotImplementedException ();            }         }        //... Omit several code     }

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

(2) realize the ItemsControl of horizontal virtualization layout.

To implement ItemsControl's horizontal virtualization layout, in addition to using the horizontal layout of the Itemsstackpanel control, You also need to set scrollviewer.horizontalscrollbarvisibility= "Auto" in ItemsControl so that the list can scroll horizontally. The code for the list is as follows:

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

MainPage.xaml File Main code------------------------------------------------------------------------------------------------------ ------------    <itemscontrol x:name= "List" >         <!--Use the Itemsstackpanel control as a layout panel for ItemsControl-->         <ItemsControl.ItemsPanel>             <ItemsPanelTemplate>                 <itemsstackpanel orientation= "Horizontal"/>             </ItemsPanelTemplate>         </ItemsControl.ItemsPanel>        < itemscontrol.template>            < Controltemplate targettype= "ItemsControl">                < Scrollviewer scrollviewer.horizontalscrollbarvisibility= "Auto"    scrollviewer.verticalscrollbarvisibility = "Disabled" >                     <ItemsPresenter/>                 </ScrollViewer>             </ControlTemplate>         </ItemsControl.Template>         <ItemsControl.ItemTemplate>             <DataTemplate>                 <stackpanel>                     <image source= "{ Binding image} " width=" 144 "  height=" " stretch=" UniformToFill "></Image>                      <textblock text= "{binding imagename}" ></TextBlock>                 </StackPanel>             </DataTemplate>         </ItemsControl.ItemTemplate>    </ItemsControl>

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

MainPage.xaml.cs File Main code---------------------------------------------------------------------------------------------------        ---------------public MainPage () {InitializeComponent (); List.    ItemsSource = new ItemList (); }

650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>

(3) The list of running effect 11.8, using debug debugging under the running program can see the log display list is only initialized 10 data items, the log is as follows:

/* Log Start */

The loaded Collection index is: 0

The loaded Collection index is: 1

The loaded Collection index is: 2

The loaded Collection index is: 3

The loaded Collection index is: 4

The loaded Collection index is: 5

The loaded Collection index is: 6

The loaded Collection index is: 7

The loaded Collection index is: 8

The loaded Collection index is: 9

/* Log Start */

650) this.width=650; "src=" Http://images.cnitblog.com/i/152755/201406/021426069744338.png "/>

This article comes from the deep understanding of Windows Phone 8.1 UI Control programming

Source code Download: Http://vdisk.weibo.com/s/zt_pyrfNHoezI

Welcome to follow my Weibo @wp forestry administration

WP8.1 Technology Group: 372552293

Windows Phone 8.1 Virtualization arranges layout controls

Related Article

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.