In this article I will talk about BackgroundTransferRequest in WindowsPhone, BackgroundFileTransferRequest API is part of Microsoft.Phone.BackgroundTransfer namespace.
BackgroundFileTransfer is capable of queuing up one or more file uploads or downloads over http, the file transfer executes in the background even though the application is not running on foreground.
Let's write code:
Step 1: Add three textblocks and a button.
<TextBlock Text="" Height="30" HorizontalAlignment="Left" Margin="10,15,0,0" Name="downloadStatus" VerticalAlignment="Top" FontSize="18" />
<TextBlock Text="" Height="30" HorizontalAlignment="Left" Margin="10,85,0,0" Name="downloadFileSize" VerticalAlignment="Top" FontSize="18" />
<TextBlock Text="" Height="30" HorizontalAlignment="Left" Margin="10,155,0,0" Name="downloadProgress" VerticalAlignment="Top" FontSize="18" />
<Button x:Name="download" Margin="20,0,0,0" Height="80" Width="430" Content="Download Video" Click="download_Click" />
Below are methods, properties and events in Microsoft.Phone.BackgroundTransfer API.
Step 2: Create three class level variable in MainPage.xaml.cs.
videoDownloadUri to hold the download path of video to be downloaded saveLocationURI to store the downloaded video in isolated storage. backgroundTransfer request object
public partial class MainPage : PhoneApplicationPage { private Uri videoDownloadUri = new Uri(http://ie.microsoft.com/testdrive/Graphics/CanvasPad/fish.mp4); private Uri saveLocationUri = new Uri("shared/transfers/MyDownloadedVideo.mp4", UriKind.RelativeOrAbsolute); private BackgroundTransferRequest backgroundTransferRequest = null;
Step 3: Add OnNavigatedTo method in code behind like below which will initialize the transfer if it is not initialized using InitializeFileTransfer method.
UpdateDownloadStatusOnUI will update the the status of download whenever user visit back the application after moving out.
UpdateDownloadProgressOnUI will update the progress of download whenever user visit back the application after moving out.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); foreach (BackgroundTransferRequest request in BackgroundTransferService.Requests) { backgroundTransferRequest = request; break; }
InitializeFileTransfer(); UpdateDownloadStatusOnUI(); UpdateDownloadProgressOnUI(); }
Step 4:InitializeFileTransfer method will initialize the BackgroundTransferRequest events.
private void InitializeFileTransfer() { if (backgroundTransferRequest != null) { backgroundTransferRequest.TransferProgressChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferRequest_TransferProgressChanged); backgroundTransferRequest.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferRequest_TransferStatusChanged); } }
Step 5: Add Microsoft.Phone.Tasks using directory to use the MediaLibrary.
using Microsoft.Phone.Tasks;
Step 6: Below method update the UI with transfer status, once the status is completed MediaPlayer will play the file.
private void UpdateDownloadStatusOnUI() { if (backgroundTransferRequest != null) { if (backgroundTransferRequest.TransferStatus == TransferStatus.Completed && backgroundTransferRequest.TransferError != null) { downloadStatus.Text = backgroundTransferRequest.TransferError.Message; } else { downloadStatus.Text = backgroundTransferRequest.TransferStatus.ToString(); } if (backgroundTransferRequest.TransferStatus == TransferStatus.Completed) { MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher(); mediaPlayerLauncher.Media = saveLocationUri; mediaPlayerLauncher.Show(); } } }
Step 7: Add below method will update UI with the progress of bytes transferred and the total file size of the file downloading.
private void UpdateDownloadProgressOnUI() { if (backgroundTransferRequest != null) { downloadFileSize.Text = "Total File Size: " + backgroundTransferRequest.TotalBytesToReceive; downloadProgress.Text = "Bytes Received: " + backgroundTransferRequest.BytesReceived; } }
Step 8: Place below event handlers which will be triggered when download status and download progress changes.
private void backgroundTransferRequest_TransferStatusChanged(object sender, BackgroundTransferEventArgs e) { UpdateDownloadStatusOnUI(); }
private void backgroundTransferRequest_TransferProgressChanged(object sender, BackgroundTransferEventArgs e) { UpdateDownloadProgressOnUI(); }
Step 9: Place the download_Click method will be start downloading of file. This method will also intialize file transfer and start updating UI with download status and progress.
private void download_Click(object sender, RoutedEventArgs e) { if (backgroundTransferRequest != null) { BackgroundTransferService.Remove(backgroundTransferRequest); }
backgroundTransferRequest = new BackgroundTransferRequest(videoDownloadUri, saveLocationUri); backgroundTransferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery; BackgroundTransferService.Add(backgroundTransferRequest); InitializeFileTransfer(); UpdateDownloadStatusOnUI(); UpdateDownloadProgressOnUI(); }
Step 10: Now run the application and click or touch on Download Video button to start downloading of video. Once the video is downloaded it will start playing itself.

Important points:
1. HTTP and HTTPS is only supported, FTP is not supported. 2. Local path should be in root directory "/shared/transfers" of the Isolated Storage for background transfers. 3.Test the app which uses BackgroundTransfer on device because emulator is always connected to Wi-Fi and external power.
Read Background Transfer Policies
This ends the article of Background File Transfers in Windows Phone.
|