This example shows how to use the background transmission service to download files in the background, even if the application is no longer running in the foreground.
Upload and download using backgroundtransferrequest
// Create the new transfer request, passing in the URI of the file to // be transferred.BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);// Set the transfer method. GET and POST are supported.transferRequest.Method = "GET";
You can use transferpreferences to set whether to use WiFi for download and whether to use it for download during charging.
// If the WiFi-only checkbox is not checked, then set the TransferPreferences // to allow transfers over a cellular connection. if (wifiOnlyCheckbox.IsChecked == false) { transferRequest.TransferPreferences = TransferPreferences.AllowCellular; } if (externalPowerOnlyCheckbox.IsChecked == false) { transferRequest.TransferPreferences = TransferPreferences.AllowBattery; } if (wifiOnlyCheckbox.IsChecked == false && externalPowerOnlyCheckbox.IsChecked == false) { transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery; }
Use independent storage to keep files
// In independent storage, determine whether a file already exists. If yes, delete using (isolatedstoragefile isostore = isolatedstoragefile. getuserstoreforapplication () {string filename = downloadfile; If (isostore. fileexists (filename) {isostore. deletefile (filename );}}
Class backgroundtransferrequest attributes:
Transferstatus transmission status
Totalbytestoreceive File Size
Bytesreceived size of the received File
Download Code:
Http://code.msdn.microsoft.com/wpapps/Background-Transfer-ce07e86f