Add Silverlight control without blocking the ui - silverlight

How can i add a control to a Silverlight Grid control without blocking the user interface.
I am creating a complicated Chart control and when i call myGrid.Children.Add(myChart) the whole page is blocked and not responding.
Any idea guys?

A few things you could try:
Wrap your call to add the chart inside of Dispatcher.BeginInvoke([add chart]). This will cause the chart to not be added until the current round of UI work is done and may help it be more responsive.
Add the chart without any data to the UI. Then in the background load the data and use bindings to have the data rendered in the chart.
Try to figure out how you can break apart your complicated chart. So maybe you can add the chart first, then add some of the other items as you go.
Make sure your data for the chart is already loaded before you add the chart. I'm assuming you're already doing this, but just want to double check.
Those are my only ideas.

Have you tested whether this is a XAML rendering issue or instantiating the chart control that is causing the blocking?
What happens when you set the Visibility of the chart to Collapsed and add that to the grid? Obviously you won't see it on the UI, so you'll either need to step through it via debug or simply pop up a MessageBox before and after you call the Add(myChart).
If it is the XAML rendering of the chart, I'd dive deeper into the XAML that chart control and look for optimizations (if you can, what chart control is this?).

Related

How to go to a specific place in page by code?[Windows Phone][Silverlight]

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.

Silverlight Custom Loading Screen Like MBProgressHUD

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).

Silverlight Preload Controls

Is there a way to get Silverlight databound controls to load in the background to shorten load times during another part of application use? Specifically, I have a tab control containing a datagrid that is slow to load when there are large number of columns and rows. The performance hit occurs the first time I click the tab. Is there a way to force this load on a background thread when app first opens or something similar?
Not sure this is exactly relevant but I just resolved an issue I had where I was firing up a new grid (which was already loaded but not visible). In the process of making it visible I also assign the ItemSource of a datagrid inside which - via a converter - generates controls. What I found was that although the datagrid in silverlight typically only loads rows it needs to (based on visibility) in my case the code sequence to show the grid and bind was happening too quickly and because the grid wasn't yet shown it (silverlight) decided it needed to load all the rows.
Calling UpdateLayout() prior to generating the controls and binding resolved the issue.

Transparent overlay which does not scroll with its parent

I would like to have a form which has a few controls as transparent overlays over a bitmap. This bitmap is subject to transform matrix (zoom & scroll). I'm trying to achieve a look similar to GoogleMaps where the controls do not move when the background image is panned/zoomed.
I've tried to mimic this in my OnPaint. However, when the window is scrolled only the newly exposed area gets invalidated so my control doesn’t repaint.
I've tried to calculate where the old control was, invalidate that area, and also invalidate the area where it's supposed to have been. When I do this it flickers and you can still see the image as its scrolled.
I tried to put a ButtonControl on my display window. However, it always scrolls with its parent control. I tried to capture the scroll events and then adjust the position of the ButtonControl. This also has a delay update effect so it looks not so good.
Any ideas would be greatly appreciated.
It sounds to me like you need to Invalidate() your control wich handles the OnPaint event.
Unfortunately, you get the flicker because the Auto-scrolling mechanism sets its position, and then you restore it. The result is two messages being sent to the button.
Place your bitmap and scroll logic in a separate control that fill the entire form. That means both your bitmap control and the button are child controls of the form.
Alternatively, draw the button yourself. You will then of course need to do some work on getting it to respond to mouse clicks etc. The ControlPaint class has methods that help you mimic the appearance of Windows controls.

WPF Performance Degradation During UI Render

I have the following components in a WPF application:
(1) Window
(2) ContentPresenter in the Window that is bound to a property in the underlying ViewModel. This Property references another ViewModel.
(3) A DataTemplate for the ViewModel that will be bound to the ContentPresenter referenced above. This data template instantiates a third-party grid that displays some data.
Whenever the ContentPresenter renders the data from the DataTemplate, it takes approximately three to four seconds for the UI to render. This causes the UI to hang for the duration of the time that it takes to render the content. Since I have little to no control over how the third-party control renders itself - my question involves whether or not it is possible to render content in a way that the UI will not hang.
Please advise.
Thanks.
Chris
How many rows is the grid displaying? And how many of those rows are visible on screen?
I'm asking because it's possible that you've got a UI layout that defeats virtualization. Usually, controls that show a scrollable list of data will perform virtualization. (The built-in ListBox does this, and any 3rd party grid of tolerable quality should do the same.) This is critical for performance, because it means your UI only needs to instantiate those items that are actually visible, rather than everything in your list.
But it's relatively easy to defeat this virtualization by accident. One way is to wrap the list or grid control in a ScrollViewer. You need virtualizing controls to be able to manage their own scrolling for virtualization to work, so the scrolling needs to happen on the inside. Wrapping a control in a ScrollViewer prevents it from doing its own scrolling. Another way it can go wrong is if you plug in a different ItemsPanel. A third possibility is that your list/grid control actually needs to be told to use virtualization.
But if you're using a control that simply takes a long time to render just the stuff you need to show on screen, then there's not much you can do - you'd need to contact the control vendor, or consider using a different vendor...

Resources