Workaround for screen flicker - c#

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.

Related

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.

WP8 Xaml UserControl performance

When creating a control in xaml, I could do two things:
Create a StackPanel, add a few TextBlocks, a Rectangle et.c
Create a templated control, and add that to my MainPage.xaml
In terms of performance (only), which one would be better? Both work fine, as I see it.
Typically, at the top of a page, I would create a kind of header panel, containing some text blocks, some visual indicator et.c. The control isn't that big, perhaps it contains 10-12 children. Usually, I just type it up directly in the page xaml, as it isn't reused anywhere. But I usually apply some kind of animation to the header panel (let's say it is part of a FeatheredTransition when loading the page).
So my question is: would there be any performance advantage (from a xaml loader/renderer/animation perspective) if I create a template control first? What's the general recommendation?
I don't think there will be any noticeable difference, so use whichever you want.

Clear PivotItem cache

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>

Dynamically create tab and inject a view in it

I use AvalonDock to have a vs-like docking style, and I use prism to inject views in my application.
Basically the user will have a panel on the side with one button per view loadable, and each time he presses one, I want to create a new tab on my AvalonDock's DocumentPane, and inject the right view in it.
Now I was under the impression that prism is good for this kind of scenario, but I can'f figure it out.
Also, I stumbled accross DocumentPane and DockablePane region adapters by Markus Raufer, have added it to my project and registered the mappings in the bootstrapper.
This allows me to compile the solution (so I'm guessing I'm getting close).
So far, I do this:
In my Shell I have a place with:
<avadock:DocumentPane avadock:ResizingPanel.ResizeWidth="500" Background="Transparent"
prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainTabControlRegion}">
</avadock:DocumentPane>
In my code-behind, after Bootstrapping etc, I have a place that does:
m_RegionManager.Regions[RegionNames.MainTabControlRegion].Add(oneOfMyUserControls);
Executing this line doesn't show anything more to the user, but if i execute it twice, it will make an error saying a view is already registered (so it did add something!).
Furthermore, when i put a regular ContentControl, mark it with prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainTabControlRegion} then add a UserControl in it the very same way I add it to AvalonDock's DocumentPane, it works. freaky
What am I doing wrong?
Thanks,
bab.
EDIT:
If define the MainTabControlRegion like this, the first view actually gets added, now id i add another one (different UserControl) it doesn't show it, but WPF Inspector says it's there.
From my point of view, it is indeed adding UserControls to a region, but it doesn't know how to create a new tab for each UserControl I add to the DocumentPane.
Since I'm not specifying anything about that, I'm probably missing a piece of code?
<avadock:DocumentPane avadock:ResizingPanel.ResizeWidth="500" Background="Transparent">
<avadock:DocumentContent Title="" prism:RegionManager.RegionName="{x:Static inf:RegionNames.MainTabControlRegion}">
</avadock:DocumentContent>
</avadock:DocumentPane>
I want it to create a new tab each time i add a usercontrol and insert it in there.
EDIT 2: Solution --> Here
This problem can be caused for many reasons.
Or the RegionAdapter is not well suited for the user controls you are trying to add, therefore I would recommend you to have a look at RegionAdapter implementation so you can have a good understanding of how it adapts your view in the region and customize it to fit your needs.
Or the user control you are adding has a particular behavior and is bugging the RegionAdapter when it tries to add your view to the DocumentPane items. Therefore you can customize the RegionAdapter or fix the user control behavior.
Or it is simply a data binding problem. The fact that nothing shows up on first execution is kind of strange. Try to inspect your DocumentPane property Binding with WPF Inspector. It saved my life more than once when dealing with custom RegionAdapters.

In Gtk, how do I remove the shadow from a Statusbar?

If I make a Statusbar, and PackEnd a Label to it, it looks something like this:
The Shadow disappears over the Label, but remains over the rest of the Statusbar. I want to remove the Shadow from the entire Statusbar. The PyGTK documentation mentions a property called shadow-type, but it's readonly, and nowhere to be found in GTK#. How do I get rid of this shadow?
You're not supposed to pack stuff into the statusbar, it's not meant as a general container.
To display text in a status bar, use its own API, i.e. gtk_statusbar_push() from C. The GTK# docs on go-mono.com seem to be offline, so I couldn't link to those right now.
shadow-type is a style property, which means it's actually supposed to be set by the user and/or desktop theme. You can set it by writing a custom style file for your application and reading it in using gtk.RcStyle.
If you have a need for two separate status messages in your application, you could also consider packing two status bars into an hbox.
The first child of a GtkStatusbar is a GtkFrame which gives the shadow border (edit: apparently not). You should be able to do this:
statusbar.get_children()[0].set_shadow_type(gtk.SHADOW_NONE)
Several apps (most notably Epiphany and, in the past, Galeon) actually replace the contents of that frame with an HBox so they can add stuff to the statusbar.

Categories