I am working on a WPF application where I need smooth animations and transitions between pages.
On startup, I instantiate all my controls, with Visibility : Collapsed, and when I go from a control to another, I animate the two controls and change the visibility.
My problem is on a control with several images : the first time I display it, we don't see the animation (probably because it loads the images in the same time). When I load it a second time, there's no problem.
Is there a way to preload this control in order to always have smooth animations ?
In the codebehind you can have an event handler OnLoaded() for when the your control is loaded for the first time. Here you can load in the images or even have them start as Visible so they are loaded and at the end of the handler, have them go back to being Collapsed.
Related
I have a WPF application running on a Win10 desktop using the new (Microsoft.Toolkit.Wpf.UI.Controls.WebView v5.0.0.0) WebView control in a dialog window. The first time this dialog window is created, WebView navigation successfully completes but most of the time the WebView stubbornly continues to display a blank page.
If I minimise that first instance of the dialogue window and restore it, the content is instantly rendered. If I close that window instance and create a new one, the control generally renders as expected.
Changing the WebView.Visibility in code to Collapsed and then back to Visible on navigation completion doesn't fix the blank page.
Has anyone seen this behaviour? Does anyone have a solution to provoke WebView to actually render?
In theory the new WebView is a better architectural option than reverting to the old WebBrowser so I'm loathe to go down that path.
Additional detail
Windows 10 desktop, x64, targeting .NET 4.6.2, WPF 4.x.
The WebView instance is defined in XAML (without a Source binding) inside a UserControl.
The UserControl is embedded in a window defined in XAML, which only contains a root level Grid to contain the UserControl.
That window is shown via ShowDialog().
Source navigation is performed in code behind controlled by the current selection of a TabControl.
The WebView is NOT defined in the TabControl item template (doing so throws exceptions most of the time on tab selection change). It is in a container that is a sibling to the TabControl.
The NavigationCompleted event reports success.
I found that attempting to minimise and then restore the containing window in code didn't fix it, despite minimise & restore working when the user does it. Invalidating the control layout or visual or arrange didn't work. Maximising and restoring the window in code did, but is visually annoying, but it led me to two reasonable workarounds:
Configure the WebView.Visibility to be Collapsed in XAML and either:
handle the UserControl.Loaded event and set the visibility to Visible; or
handle the WebView.NavigationStarting event and set it to Visible.
Those are easiest if starting out collapsed won't cause unacceptable visual disruption. Getting in early via the Loaded event helps. Starting out Hidden doesn't work.
Leave WebView.Visibility configured to Visible in XAML, handle the WebView.NavigationStarted event in the UserControl, and then if it's the first time the handler called in this process do something like this to cause the control to resize and then restore the original size:
var height = Height;
Height = 0;
Application.Current.Dispatcher.BeginInvoke(
(Action)(() => { Height = height; }),
DispatcherPriority.Input
);
Dispatching the value restoration seems to be required (although maybe calling something like InvalidateLayout() in between would suffice).
In Silverlight for Windows phone, I need to go to a specific place in a page by code (hopefully with animation). The requirement is a bit like navigate to a specific anchor in HTML using url such as http://url#anchorname. However, I don't know how to do it in Windows Phone. Any ideas?
What do you mean by "go"? You could try setting the focus on a control calling control.Focus(). If you want to just scroll a ScrollViewer or a ListBox to display a control that is inside of it - WPF had a BringIntoView() method that some people ported to Silverlight, eg. here.
If you want animation - you will need to add an attached dependency property that will allow to run an animation updating the scroll offset using ScrollToVerticalOffset(). Then have the BringIntoView implementation - instead of just jumping to the offset using ScrollToVerticalOffset - run an animation that will update the attached dependency property and smoothly animate calling ScrollToVerticalOffset many times.
Just wondering if anybody has any advice on making a custom loading indicator/HUD for silverlight that resembles the MBProgressHUD for the iphone.
https://github.com/jdg/MBProgressHUD
All I want is a busy indicator, like the one that comes with the silverlight toolkit but to style it like the MBProgressHUD with a spinning loading indicator.
Any ideas?
Thanks
Just create a custom control with appropriate animations and elements arranged in a circle (see rotate transforms).
You may wish to use a timer in the code behind but if you do, be sure to un-hook the event handler when the progress indicator is no longer displayed or you will leak memory.
If you then want to use it as the one in the silverlight toolkit just use your control in a controltemplate for the busy indicator in the silverlight toolkit. Alternatively just create your own version of the control (Alignment set to center, visibility to show/hide, a background which fills the screen if you need it to disable clicking on the form behind, but it as the last item in your layout-root so it's on top).
I have just discovered that UserControls in a TabControl will not load until the parent TabPage is selected. Is there a way to prevent this delay loading? I need to initialize the UserControls when the main form loads.
The TabControl does not treat its controls specially, in fact it is normal under all circumstances for the Load event on a UserControl to occur immediately before the control is displayed for the first time. The TabPage is responsible for showing the control, therefore it will only be 'loaded' when first selected.
To overcome this (perfectly normal) Windows Forms behaviour, you could move your initialisation code to a separate method and call it when the Form loads, or you could just place your initialisation code in the UserControl's constructor instead. Either way, you can perform your initialisation immediately.
You can invoke Tabcontrol's SelectTab() method for the tabs in your Form's Load event handler.
I was just searching how to achieve this default behaviour you describe. An application I support was not delaying the load of the tabs. Turns out the tabs were getting initialised in the load event instead of the constructor.
So if you add the tabs to the tabcontrol in the form load event all the controls in tabs will have their load events fired as a part of the TabPages.AddRange call
I have WPF ListBox that shows a lot of data. I need smooth scrolling, so I've set ListBox.ScrollViewer.CanContentScroll to False that disables virtualization. Now when I open the tab where this ListBox is placed, I see nothing for few seconds because ListBox is loading/creating items/rendering. I also have a control that shows some animation that indicates that application is running and user should wait a bit.
How can I show this control while ListBox is not available?
Add a Grid in the location of your list box and place inside it your ListBox and your animation control. This way they are placed in the same location. The animation control should be on the top of the z-order and so displayed. Once the ListBox has finished loading you would then hide the animation control and so the ListBox would show instead. Any time you need to perform another long operation you set the animation control to visible again.
Clean shutdown in Silverlight and WPF applications
Check how the author of this application did it via code maybe it can help you though it is different scenario.