此示例显示了如何使用后台传输服务在后台下载文件,即使当应用程序不再运行在前台。
通过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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步