WP7 Listbox how UI virtualization work - windows-phone-7

I'm using ListBox which has VirtualizingStackPanel, which is said to support UI virtualizing by default.
However, when I set my listStudent (of type ObservableCollection, and have 5 Students in it) as ItemsSource for my Listbox. Then whenever user scroll to the end, I add another 5 Students to my listStudent (and of course UI is notified). But I see that memory consumed keep increased. There's no different from StackPanel in term of memory
How UI virtualization work? How to keep memory low when adding new item to listStudent?

virtualization means list do not construct the list items which you have not scroll to yet.
so you can put your test code to item's loaded function or used converter code. you can see whether virtualization works
for example
you can bind a converter to student's name prop and you can log it .then you know when the item create indeed

Ensure that you don't modified ListBox ItemsPanel that destroys UI virtualization

Sheldon_Xiao on has pointed out some references on MSDN that helpfully explain UI virtualizartion

Related

IsEnabled Binding performance concerns?

I am dynamically loading around 1000+ items which are translated to buttons in xaml.
The view model has an IsEnabled property that is binded to the IsEnabled property of the button in the View.
My question is, won't this affect performance? I only need the property on some of the items but all of them use the same viewmodel and it is quite hard to introduce a new view model just for that purpose.
I am using Windows Store 8.1.
Thank You!
1k bool bindings should not do any harm. I've seen applications with 5-10k bindings beeing populated at once, they can freeze a little bit if done in sync. Your item/list in viewmodel loading should be done async to avoid UI freeze if the collection of those items is bound instantly with all the items already there, that is why observable collection is a fundamental thing to bind to.

WP7 - Implementing a tree of comments. ListBox, Tree controls, etc?

I want to implement a screen to display a tree of comments in WP7. Each comment can have children comments and so on. Each child comment will be visually distinct from its parent via indenting
ie:
"comment text"
"Some child comment text"
"Some child comment text"
"some child comment text"
"comment text"
"Some child comment text"
What would be the best way to go about implementing this? I'd like to keep the implementation as simple as possible so initially I was thinking I could use a single ListBox and programmatically set the Padding/Margin of each comment/ListItem, depending on its depth in the tree. But I can't seem to get it working. Any examples, suggestions, recommendations, etc?
Edit: Doing some additional reading, it seems like a DataTrigger would have been perfect for this sort of thing http://www.codeproject.com/Articles/113152/Applying-Data-Templates-Dynamically-by-Type-in-WP7 ...But WP7 doesn't support triggers.
One other idea I had was to make the Margin/Padding a Property of the Comment class, and then databind to that...this should work, but I'm contaminating my Comment class with display information. Any ideas on how I could databind the Margin/Padding value but somehow not mix model & view codes?
You'll have to roll your own, either from scratch, or by assembling something out of existing controls. ListBox looks like a good bet for this purpose.
Take a peek at this MSDN thread (web archive - thread now moved/deleted), it has several suggestions about simulating a TreeView using a ListBox, and a claim (which I have not verified) that you can use System.Windows.Controls.TreeView in WP7 (with the caveat that you also need System.Windows.Controls.Toolkit).
The marked answer, written by Shaun Taulbee:
Tree view behaviour in a listbox could be mimiced with a bit of smarts in the collection handling. Features your classes would need to support that come to mind are
a collection whose elements supports retrieving child collections
similarly to be able to detect if an element has child collections
in the data template for the listbox show one element of the stackpanel for expansion state based on presence of children and whether or not expanded
in the data template for the listbox show one element of the stackpanel for indent which reflects the depth of the child - to accomplish this best you should have a collection that represents a flat version of your tree data to bind to - then when you insert items you can make the indent based on the indent of the parent item that was just clicked
when a node is clicked in the listbox you insert the children from that node into the flat collection that the listbox is bound to
when the node is clicked again the children are deleted from the flat collection
You could encapsulate all of this into some neat classes to provide a fairly simple reusable api I would imagine if you wanted.
This thread has a fair amount of noise, but down at the bottom there's a comment from Mark Chamberlain:
"TreeView is not a natural fit for the phone, you can emulate Treeview
in other ways, for example, with ListBox item templates, Pivot or with
other List patterns. It will depend on how many levels of the tree
you will have.
"For example, you can template your ListItem to contain a label and
another Listbox with same item template. Doing this you can emulate
as many drill in levels as you need to handle, but only one branch at
a time."
"You may be able to re-template the TreeView (source is also available
in the Silverlight Toolkit), but it isn’t a supported scenario, and
you would need to do a decent amount of work to get it looking good on
the phone from a design & re-templating standpoint."
The following should be good starting points, both altering the ItemTemplate for a ListBox control :-
http://3water.wordpress.com/2010/07/25/listbox-on-wp7/
http://weblogs.asp.net/psheriff/archive/2010/10/27/windows-phone-list-box-with-images.aspx
For WP8 you can use this one TreeView_WP8

Updating Controls from Multiple Pages on Windows Phone

All, I am new to Windows 7 Phone. My situation is that I have a main page which contains a ScrollViewer which in turn houses a StackPanel. I want to populate this StackPanel with multiple sub-StackPanels (at runtime) which are to hold an Image Thumb nail a hyperlink and some basic information about the image.
This is all good when I do this from the main page, but I want to know how to update this control (which is on the main page), but from any page other than the main page. I would like to know what is considered best practice for updating a page's control (like that outlined above) from another page.
Obviously there are a number of ways to pass data between pages
PhoneApplicationService.Current.State["yourparam"] = param
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));
then in other page simply
var k = PhoneApplicationService.Current.State["yourparam"];
and many others. But what is best practice for updating a generic control from a different page?
Note: There are many question about data access and passing between pages.
Passing data from page to page
How to pass the image value in one xaml page to another xaml page in windows phone 7?
Passing image from one page to another windows phone 7
and more. This is not what I am asking.
If I understand your question correctly, you are trying to update a control which is on for example MainPage.xaml from another page for example Page2.xaml.
As far as I know there is no way to reach a pages controls from another page, and that seems unnecessary for the cases that I can think of.
The method used to achieve what you are trying is usually done by triggering an action (like the press of a button ) and passing a parameter to the page you are trying to update the control. And on that page's onnavigatedto event (or viewmodel constructor if you are using the MVVM pattern), update your control based on the passed parameter.
If your update is based on data then the best practice is to bind an observable collection or an object that extends the INotifyPropertyChanged (basically any object that can signal that one of their property changed to the ui) and change the data based on the parameter that is passed.
If these two pages somehow are visible at the same time and there is no navigation needed between them( like a popup or sliding menu kind of ui) then you can make the page that you are showing in the popup a usercontrol, and reach to the parent's controls by this.Parent.
I can be more helpful if you give more specifics about your app's flow.
The MVVM pattern would be a good way to go. Saying MVVM is too complicated for small teams isn't exactly accurate - the purpose of MVVM is to decouple Silverlight or WPF code. Using the codebehind of a Silverlight page to directly access data creates coupling in your code and accrues technical debt. Whether you're one developer or 100, if your UI is coupled with your data classes, if you have to change your data classes, you will have to make changes to every UI element that uses those classes. This takes longer and makes your application more difficult to change.
MVVM makes it so your UI (the View) doesn't know anything about the data (your Model). The ViewModel is the code in between that the UI can bind to, and which manages events in the UI that need to be persisted to the Model, and also changes in the Model that need to be represented in the View. For this reason, it handles events, and that's what it sounds like you need in your code - an event that can exist off of the codebehind, that can update the Views bound to it when the data changes. If you have two pages, then an event on one of the pages will be sent to the ViewModel, which will make a change to the Model (data) if necessary, and pass it back to the ViewModel. The ViewModel would then update any of the UI elements (Views) bound to that piece of data.
There's a REALLY good demonstration of how to implement the MVVM design pattern here
. The guy goes through and takes a typical WPF application (just like Silverlight), where the UI codebehind implements event handlers that directly access data, and refactors it using the MVVM pattern.

UI Design of listboxes in windows phone 7?

in a page 4 menus one by one like the below.
APPS
1.sample1
2.sample2
PERSONAL
1.info1
2.info2
Valuble
1.item1
2.item2.
More
1.item1
2.item2
like the above APPS,PERSONAL,Valuble,More these are Headings.in that items are binding from Sqlite DB.
intially there are no items .DB is creating .by click on button in that page navigating to another page here i'm binding categories[APPS,PERSONAL---] to combobox and one textbox is there.in that way i'm adding items to mainpage.
functionalty is ok but i taken four listboxes statically placed in that i'm binding the items.but intially no items while db creation.if whenever adding the PERSONAL item first the first listbox was empty and filling the second listbox.if whenever adding MORE items first ,the above listboxes are empty this is bad design i think.
How to design UI for that...
I would have thought this is is an ideal candidate for the LongList selector control of Windows Phone - similar to the contacts page.
Have a look here http://www.windowsphonegeek.com/articles/wp7-longlistselector-in-depth--part1-visual-structure-and-api
I haven't used it myself but would think that you should be able to use it to get the effect you require.

How to bind test data to a SketchFlow ComboBox?

I haven't been able to successfully bind some test data to a SketchFlow ComboBox. I added a sample data source and created a collection with some basic string values. There are a number of tutorials on how to bind to a ListBox in SketchFlow and that works fine, just not finding anything for a ComboBox.
One thing that caught me out a few times is that you need to have your data in "list mode" before you drag it on to the control (combo box or list box).
It seems to work as I expected, so perhaps I'm not understanding your issue correctly, but I'll give it a shot! I created a sample DataSource with a couple of strings in it, added a ComboBox to the layout root and then just dragged the Collection from the DataSource and dropped it on the ComboBox.
Keep in mind that if you drag the DataSource itself, you'll only be setting the DataContext property of the ComboBox and that's not enough to get your items to display. You also need the ItemsSource bound to the Collection of the DataSource and an ItemTemplate or a DisplayMemberPath to tell the ComboBox how to display your items.
Additionally, if you use the drag and drop method of binding, it will use the ItemTemplate approach for you, which may or may not be what you want as it will generally create a StackPanel and display all fields from the row in your Collection per item in the ComboBox. You can remove the ItemTemplate and set DisplayMemberPath to whichever field you wish to have displayed from your data source's collection.

Resources