此示例显示了如何使用后台传输服务在后台下载文件,即使当应用程序不再运行在前台。

通过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";


通过TransferPreferences设置,是否用Wifi下载,是否充电时下载

 // 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;
            }

 

使用独立存储保持文件

//在独立存储中,判断是否文件已存在, 存在则删除
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string filename = downloadFile;
                    if (isoStore.FileExists(filename))
                    {
                        isoStore.DeleteFile(filename);
                    } 
                }

类BackgroundTransferRequest 属性:

TransferStatus 传输状态

TotalBytesToReceive 文件大小

BytesReceived 已收到的文件大小

 

代码下载:

http://code.msdn.microsoft.com/wpapps/Background-Transfer-ce07e86f

posted on 2013-04-11 10:49  微星  阅读(262)  评论(0编辑  收藏  举报