|
In this article I will explore how to show download percentage and processing when we start downloading image in Windows Phone 7. I have uploaded a image of size around 745KB, so that it takes some time to download and we can see how it works.
Let's write some code.
Step 1: Add a textblock, two progressbar and an image with border inside contentpanel grid in MainPage.xaml.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel x:Name="stackPanel1" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock x:Name="downloadPercentage" HorizontalAlignment="Center" VerticalAlignment="Center" /> <ProgressBar x:Name="progressPercentage" Width="350" Height="25" Minimum="0" Maximum="100" /> <ProgressBar x:Name="processing" IsIndeterminate="True" Width="350" Height="25" /> </StackPanel> <Border BorderThickness="2" BorderBrush="Silver"> <Image x:Name="image1"> <Image.RenderTransform> <TransformGroup> <ScaleTransform x:Name="scale" /> <TranslateTransform x:Name="translate" /> </TransformGroup> </Image.RenderTransform> </Image> </Border> </Grid>
Step 2: Create a bitmapimage object and attach DownloadProgress event handler to it which will be triggered when download starts.
public MainPage() { InitializeComponent(); string source = http://dotnetspeaks.com/images/IMG_0413.JPG; BitmapImage bitmapImage = new BitmapImage(new Uri(source, UriKind.Absolute)); bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress); image1.Source = bitmapImage; }
Step 3: bitmapImage_DownloadProgress will update the progressbar to display download in progress. e.Progress gives the value of the percentage which can be shown on screen by converting into string.
void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e) { progressPercentage.Value = e.Progress; downloadPercentage.Text = "Downloading.... " + e.Progress.ToString() + "%"; }
Now run the application to start downloading the image in Windows Phone 7.

Once the image is download it will appear in the screen like below.

This ends the article of showing progressbar while downloading image in Windows Phone 7.
|