Hide WP7 progress bar when the image is loaded - silverlight

I'm using a progress bar for loading an image in an application for Windows Phone 7.
bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs (bitmapImage_DownloadProgress);
void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
{
progressBar.Value = e.Progress;
}
XAML file:
<ProgressBar x:Name="progressBar" IsIndeterminate="True" Width="300" Height="30" Style="{StaticResource PerformanceProgressBar}" />
I'd like to know the best way to make the progress bar disappear once the image is loaded.

Use the events ImageOpened which triggers when image has been loaded successfully and ImageFailed which triggers if image failed to download.
bitmapImage.ImageOpened += new EventHandler<RoutedEventArgs>(bi_ImageOpened);
bitmapImage.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageFailed);
void bi_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show("Download of image failed");
progressBar.Visibility = Visibility.Collapsed;
}
void bi_ImageOpened(object sender, RoutedEventArgs e)
{
progressBar.Visibility = Visibility.Collapsed;
}

Simply hide it via the Visibility property
progressBar.Visibility = Visibility.Collapsed

Related

Keep submenu open when a new image is selected for display

Using WPF: A Simple Color Picker With Preview, Sacha Barber, 18 Apr 2012 ,
I created a custom control from it:
public class ColorCustomControl : Control
{....}
It is then used as:
<Menu....>
<MenuItem.....>
<pn:ColorCustomControl/>
</MenuItem>
</Menu>
This yields the following picture when the Brushes MenuItem is selected:
Selection of any item in the opened Brushes submenu results in the appropriate action being taken with the Brushes submenu REMAINING OPEN. This is the effect I want.
However, as shown below, selection of any of the three swatches results in a quick flicker of the new swatch -- it replaces the color pattern to the left of "Preview"--followed immediately by closure of the Brushes submenu.
If the Brushes menuitem is again chosen, the most recently selected swatch correctly appears.
I have tried all preview events (i.e., keyboard lost focus, left mouse down, etc.), to try stopping closure of the submenu when a swatch is chosen. Nothing I have found will stop the popup from closing.
How can closure of the Brushes submenu be prevented when selecting a swatch from the visual?
(I strongly suspect that redrawing of the visual, as in InvalidateVisual() when a new swatch image is selected, is forcing closure of the submenu).
Any ideas anybody?
TIA
My suggestion is to stop events propagation from your user control. So in your ColorCustomControl class first of all add a property (it can be a dependency one too if you need):
private bool propagateEvents = true;
public bool PropagateEvents
{
get
{
return propagateEvents;
}
set
{
propagateEvents = value;
}
}
Then add e.Handled = !PropagateEvents; at the end of every mouse event handler; in the end add a Swatch_MouseLeftButtonUp method (it has to handle the event raised by ImgSqaure1, ImgSqaure2 and ImgCircle1).
The result will be:
private void Swatch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Image img = (sender as Image);
ColorImage.Source = img.Source;
e.Handled = !PropagateEvents;
}
private void Swatch_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = !PropagateEvents;
}
private void CanvImage_MouseDown(object sender, MouseButtonEventArgs e)
{
IsMouseDown = true;
e.Handled = !PropagateEvents;
}
private void CanvImage_MouseUp(object sender, MouseButtonEventArgs e)
{
IsMouseDown = false;
e.Handled = !PropagateEvents;
}
and in the user control XAML:
<Image x:Name="ImgSqaure1"
Height="20" Width="20"
Source="Images/ColorSwatchSquare1.png"
Margin="45,0,0,0"
ToolTip="Square swatch1"
MouseLeftButtonDown="Swatch_MouseLeftButtonDown"
MouseLeftButtonUp="Swatch_MouseLeftButtonUp"/>
<Image x:Name="ImgSqaure2"
Height="20" Width="20"
Source="Images/ColorSwatchSquare2.png" Margin="5,0,0,0"
ToolTip="Square swatch2"
MouseLeftButtonDown="Swatch_MouseLeftButtonDown"
MouseLeftButtonUp="Swatch_MouseLeftButtonUp"/>
<Image x:Name="ImgCircle1" Height="20" Width="20"
Source="Images/ColorSwatchCircle.png" Margin="5,0,0,0"
ToolTip="Circle swatch1"
MouseLeftButtonDown="Swatch_MouseLeftButtonDown"
MouseLeftButtonUp="Swatch_MouseLeftButtonUp" />
Now all you have to do is set the PropagateEvents property in your menu:
<Menu....>
<MenuItem.....>
<pn:ColorCustomControl PropagateEvents="False" />
</MenuItem>
</Menu>
I hope it can help you.

Silverlight Toolkit Accordion ScrollViewer

I am populating my Accordion with items from my database. I have also wrapped my Accordion in a ScrollViewer:
<ScrollViewer Name="LayoutScrollViewer">
<toolkit:Accordion Name="ItemsAccordion" ItemTemplate="{StaticResource AccordionHeaderTemplate}" ContentTemplate="{StaticResource AccordionContentTemplate}"></toolkit:Accordion>
</ScrollViewer>
However, I cannot find a way to initially show the VerticalOffset of the ScrollViewer to 0. It keeps on scrolling to the bottom whenever my database content is finished loading. I have tried in the codebehind:
void CatalogItem_Loaded(object sender, RoutedEventArgs e)
{
WebServiceClient client = new WebServiceClient();
client.GetCatalogItemsAsync(countID);
client.GetCatalogItemsCompleted += new EventHandler<GetCatalogItemsCompletedEventArgs>(client_GetCatalogItemsCompleted);
}
void client_GetCatalogItemsCompleted(object sender, GetCatalogItemsCompletedEventArgs e)
{
ItemsAccordion.ItemsSource = e.Result;
UpdateScrollViewer();
}
private void UpdateScrollViewer()
{
LayoutScrollViewer.ScrollToVerticalOffset(0);
}
This doesn't work, though. I have also tried, in UpdateScrollViewer() to do:
LayoutScrollViewer.IsHitTestVisible = false;
LayoutScrollViewer.IsHitTestVisible = true;
which doesn't work either. If I leave it as IsHitTestVisible= false, then it works as I would like; but I also want user interaction with the Accordion, so this isn't a permanent solution.
I hate to do this but thy the following:
void client_GetCatalogItemsCompleted(object sender, GetCatalogItemsCompletedEventArgs e)
{
ItemsAccordion.ItemsSource = e.Result;
UpdateLayout();
UpdateScrollViewer();
}

How to fade an image in on load silverlight?

Can anyone point me to a resource on how to "fade in" an image on load in silverlight? Basically I have a listbox of items that are returned from a web service and some times the images take a little longer to load, so I wanted to fade them in as they download. I read that I might need a storyboard for this effect. Is this the best route or are there alternatives?
I'm not aware of any alternatives. The StoryBoard is the best route. You could just animate the Opacity of the image from 0 to 100.
Put the Storyboard in the resources for the UserControl () or in the App.xaml.
Then in your OnOpened event (as you mentioned in the comment):
protected void OnOpened(object sender, EventArgs e)
{
// params might be incorrect
this.fadeInStoryBoard.Stop();
// your image controls will need x:names set
this.fadeInAnimation.SetValue(Storyboard.TargetNameProperty, ((Image)sender).Name);
this.fadeInStoryBoard.Start();
}
The example storyboard is from:
http://www.codeproject.com/KB/silverlight/AgDynAnimations.aspx
and starting the storyboard: http://blogs.msdn.com/b/silverlight_sdk/archive/2008/03/26/target-multiple-objects-properties-with-one-animation-silverlight.aspx
Here's a utility function to perform the task
private void FadeIn(UIElement uilelement)
{
uilelement.Opacity = 0.0;
uilelement.Visibility = Visibility = Visibility.Visible;
var timeline = new DoubleAnimation() { To = 1.0, Duration =TimeSpan.FromSeconds(3.0) };
Storyboard.SetTarget(timeline, uilelement);
Storyboard.SetTargetProperty(timeline, new PropertyPath(UIElement.OpacityProperty));
var sb = new Storyboard();
sb.Children.Add(timeline);
sb.Begin();
}
function void image1_Opened(object sender, EventArgs e)
{
FadeIn(sender as Image);
}
You should set the Image to Collapsed, or set its initial Opacity to 0.
<Image Source="{Binding ImagePath}"
Name="image1"
Visibility="Collapsed"
ImageOpened="image1_ImageOpened" />

Change background color of HyperLinkButton on MouseOver/MouseEnter in Silverlight

I am new to silverlight,will any one guide how can I do this.
Thanx
XAML:
<HyperlinkButton Name="hyperlinkButton" Content="Change Background" MouseEnter="hyperlinkButton_MouseEnter" MouseLeave="hyperlinkButton_MouseLeave" />
Code Behind:
private void hyperlinkButton_MouseEnter(object sender, MouseEventArgs e)
{
hyperlinkButton.Background = new SolidColorBrush(Colors.Red);
}
private void hyperlinkButton_MouseLeave(object sender, MouseEventArgs e)
{
hyperlinkButton.Background = new SolidColorBrush(Colors.White);
}
Here is video tutorial "How to Style a Hyperlink Button". There you can see
How to change the color of a Hyperlink Button when the mouse is over it.
Source code for download is also available.
If you whant to do that as #BurkDigglers described, than you must handle MouseEnter and MouseLeave events for each hyperlink button.

Slider control in silverlight

I am using slider control for an audio player in silverlight application. The slider is not moving while audio is playing.
The below one is my XAML code . How to get it?
<Slider x:Name="Slider" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2"
MouseLeftButtonDown="MouseClicked" MouseLeftButtonUp="MouseReleased"
ValueChanged="Slider_ValueChanged" Height="30" Width="484" ></Slider>
Code behind for slider events:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Audio.Pause();
Audio.Position = TimeSpan.FromSeconds(Slider.Value);
Audio.Play();
}
private void MouseClicked(object sender, MouseButtonEventArgs e)
{
Audio.Pause();
Audio.Position = TimeSpan.FromSeconds(Slider.Value);
}
private void MouseReleased(object sender, MouseButtonEventArgs e)
{
Audio.Play();
}
In Silverlight 3(as i know) MouseLeftButtonDown not working. You need to create own Handler in code. For example :
progress_bar.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(progress_bar_MouseLeftButtonDown), true);

Resources