Clear PivotItem cache - c#

I know that you aren't supposed to add more than 7 items to a pivot control, but it's just dead easy to use.
I wasn't even running into problems until now: I got a pivot item template which renders articles (Say, date, title, content). I'm using a pivot because when flicking the article I want to go the next/previous one. However, after a number of flicks (~50, little abyssmal) I get an OutOfMemoryException. The memory usage suggests, that PivotItem contents are generated on the fly when first accessed, but then they aren't released.
I could hook into UnloadedPivotItem/LoadedPivotItem (works well for image viewing and the Source property) and clear the visual trees, but that would mean that I had to generate the contents for every view model "by hand".
So: Is there any possible way to specify how big that cache should be? Or at least a sane way to regenerate the PivotItem content from a given ViewModel?

I would recommend using Telerik's SlideView control. In my mind, there is no reason to not use Telerik's control (I do not work for, nor endorsed by Telerik). You can download the Telerik Examples app from the store to see it being used. This control is exactly what you need. It works just like any other ItemsControl would. You set the ItemsSource and ItemTemplate.
<telerikPrimitives:RadSlideView ItemsSource="{Binding Articles}"
IsLoopingEnabled="False" <!-- Will NOT go back to the beginning -->
ItemRealizationMode="ViewportItem">
<telerikPrimitives:RadSlideView.ItemTemplate>
<DataTemplate>
<Grid Margin="12,0,0,0">
<!-- Content ->
</Grid>
</DataTemplate>
</telerikPrimitives:RadSlideView.ItemTemplate>
</telerikPrimitives:RadSlideView>

Related

Workaround for screen flicker

Writing a fairly simple app with c#/wpf.
When opening a second window there is a brief flicker or flash.
Extensive googling says that this caused by screen repainting, so not much I can do about that.
(I haven't included any code because it seems this problem is well known and something most people 'learn to live with')
So, I thought, why not put all the XAML in one file with each page within its own grid that I can hide/show with visibility.visible or visibility.collapsed etc.
Works nicely (apart from a very slight delay - less than 1/2 second - the first time i show the second window grid), but I'm unhappy at the idea of containing all the code behind in a single page. (for what its worth, i'm told that compiler see's it all as one file anyway but ... )
Is there a way I can create separate 'code behind files for each 'windows' (in reality a XAML Grid) functionality whilst linking them all to the same Xaml File?
(I have a horrible feeling having typed this that the answer is obvious and I should already know it ...)
Thanx
If I understand you correctly, you just need to create a new Control Class-Code Behind pair for each 'window grid'. This is done in VS by clicking Add-Create Element-User Control (of name UserControl1.xaml, rename it to something Like YourControl.xaml) in Solution Explorer (I think you already know all this, but just in case).
Now, if there is no specific requirement for the control to be of type Grid, you can use it as it is:
<MainWindow>
<Grid>
<YourControlNumberOne />
<YourControlNumberTwo />
<YourControlNumberThree />
<Grid/>
</MainWindow>
If you specifically need Grid-based control, just change the base class of YourControl to Grid, and change the topmost XAML element in the associated XAML file to Grid.

UserControl content not updating

I'm creating a simple UWP application for myself and stuck with lack of understanding of how binding and all that stuff works. Don't know how to explain it better So I created a simple example: https://gist.github.com/anonymous/744dc688d0663a3c14b7a2fc424316f8
The program has a list of selectable items on main screen. I can select several items, press a button and program will show new floating panel, with selected items (And I do further management of these items in my original program). I want this panel to be separate control because my MainPage is already overfilled with code.
The problem is. When I click on the button first time - it works as I expect. But then, when I change selected items and click button again, it shows panel with items from first selection.
My question is - how to do make it work? I have a feeling that it has to be something with INotifyPropertyChanged but cannot understand it.
And I also would be glad to hear the overall recommendations. I'm not c# developer. I'm actually sys. admin but I know a little of python and I learned Delphi 7 in university. So I'm kind of learning c# in process of creation of this application.
UPD: If I change line 28 of SelectedItemsView.xaml from
ItemsSource="{x:Bind SelectedItems}"
To
ItemsSource="{Binding SelectedItems, ElementName=SelectedItemsViewRoot}"
(and add x:Name="SelectedItemsViewRoot" to user control attributes). It works as needed. So new question arrives - is this correct way to do this? I though that Binding is kind of legacy and x:Bind is newer approach that should be used in new apps.
It's ok to use {Binding}, but if you want to stick with {x:Bind}, you can do this with OneWay mode (instead of default OneTime). For example: {x:Bind SelectedItems, Mode=OneWay}.
I strongly encourage you to read these two official tutorials:
Data binding overview and Data binding in depth.
In short, {Binding} is not legacy. It's actually a bit more flexible, but it's performed in runtime, while {x:Bind} is perfmormed at compile time, thus a bit more performant.

How to implement a customized sortable and expandable list in WPF

I need to implement a list customized like below. You should be able to sort items on a given field by clicking on a header.
A line should also be expandable. When a user clicks on a +, the line should expand and disclose more information under its field2 value.
I have tried to use a ListView in a GridView. For each column, I have defined a template (a template for field1 values with the + on the left, one for field 2 values and one for field 3 values). With this implementation, my problem is "how to expand one line".
I also looked at the ListBox component, but I need to create headers manually and align the content. I also need to sort manually. It doesn't seem to be a good solution to me.
Do you have any better idea, or any advice?
The correct base Control appears to be the DataGrid.
It will provide you with the functionality like sorting on headers, etc.
You will have to modify its ControlTemplate through a style if you want it to look like in the screenshot.
As an inspiration how you could ge the detail expansion, the following article could help you:
Grouping in DataGrid in WPF
You can also consider having a look at third party dataGrids, in case the standard DataGrid doesn't cover all of your needs, but with individual Templating you can get pretty far.
If I understand correctly, by "expand" you mean to show sth under a line (or specifically, a list view item)?
If it is the case, in your data template, add a stackpanel, for instance, and change its Visibility to collaped;
by clicking on the "+" button, change the collaped to visible.
<ListView ...>
<ItemTemplate>
<GridView ...>
<Button Name="ExpandButton Click="OnClick"/>
<StackPane Visibility={Binding SomeProperty} />
<Itemtemplate>
</ListView>
In Onclick, change the SomeProperty=Visibility.Visible. I assume you are comfortable with data binding in WPF.

Using XAML ItemTemplates with Metro UI

The default "split view" template for a Metro application written in C# makes use of an ItemTemplate attribute in the ListView tag. I'm just getting started with XAML and C#, so it's all still a bit overwhelming.
I understand that the ItemsSource is used to provide the data collection to the view, but I'm a bit confused as to how the ListView knows what to display from those model classes. Is this defined by the ItemTemlate? There's a snippet of code:
...ItemTemplate="{StaticResource SnapListItemTemplate}" ItemContainerStyle="{StaticResource SnapListTileStyle}" ItemsPanel="{StaticResource SnapListItemsPanelTemplate}"...
I don't really understand most of that line. Where are these SnapList* options defined? Is there a provided list of available templates, or are these all defined my project somewhere? Does the SnapListItemTemplate look for a specific set of ivars to display? Can this template be altered or "subclassed"?
For example, there are a number of properties on the model classes like title, subtitle, etc. And I'm curious as to how the ListView knows to pull out those specific values, and also how it knows to lay them out. What if I want to change the name of the subtitle ivar to subtext? How is the item template updated?
The SnapList* templates should be defined in your project somewhere.
The binding is {StaticResource ...} which means that the template/style or whatever is usually defined in some XAML file.
If you search the project (Ctrl+Shift+F) for the names they should turn up, probably in a file called Styles.xaml.

Dynamically generating listbox data from website, windows phone 7

I am about to begin a major step in my app by taking images I post on my website and having the phone retrieve these images and dynamically add them to a listbox. That listbox will contain a thumbnail of the image, as well as the time it was posted. If a user clicks on the image, they will be taken to another page where they can either download it to the phone or share it to someone.
However, as with any absolute beginner, I am seeking tutorials on how to do this.
I have found two:
http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
Windows Phone 7: Making ListBox items change dynamically
I'll make some mock code up in an effort to help understand my approach on this:
Have a listbox (name: photosfromsite)
Aim is to have ListBoxItems as individual photos from the site. How to populate these dynamically is yet to be done. How?
Be able to pass ListBoxItem name/identifier/etc to pass the image on new page when
clicked (use query during page transition?)
Have website set with photos.
Do an asynch call upon application launch to retrieve these images.Calling maybe 10 at
a time?
Page1 XAML:
<Grid x:Name="layoutgrid">
<StackPanel x:Name="panelforlistbox">
<ListBox x:Name="photosfromsite">
<ListBox.ItemTemplate>
<ListBoxItem>
<Image Source="http://www.thewebsite.com/Image1.jpg>
</ListBoxItem>
<ListBoxItem>
<Image Source="http://www.thewebsite.com/Image2.jpg>
</ListBoxItem>
......
......
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
Page1 C#:
//Some event handler to requisition the website for the images placed
//Some code to grab {get, set} the date and time
//Some code to generate a new listbox item in light of this.
//Some event handler for getting the updated list, if not already updated.
//Some event handler to signify when a user clicked on a listboxitem and to
//pull that info to a new page.
I realize I am missing a large portion of this code, but I think I have a somewhat passable framework in mind of what I need to do?
Any help is always appreciated. I've picked up C# for the first time a couple months ago, and slowly I've been learning a lot about it. This seems to be the only major thing in my way before my app is done, and I just want to make sure I get it right!
And to make sure no ambiguity exists in the claim about what my question is -- Are these suitable tutorials or is there somewhere with a step-by-step instruction (preferably video) of how to accomplish exactly what I am trying to do. If not, can example code be written or pasted?
Have you considered how the website will maintain a list of its images? Or are you going to scrape the HTML for them?
Are they going to be FTP'd to the site or will there be some form of DB that contains a list of them?
A webservice could then be added to the site that returns a list of all images that would then be the datasource of the list box. A convertor could be used to modify the image file name to include the full path to where the image is? I'm guessing the webservice could even be used to create the thumbnails and send them to the phone, or the phone could retrieve them and create the thumbnail.
It would be worth considering the bandwidth requirements of moving large images images about.

Categories