public BackgroundTransferControl() { InitializeComponent(); _request = BackgroundTransferService.Requests.FirstOrDefault(s => s.Tag == "Channel9Download"); if (_request != null) { _monitor = new TransferMonitor(_request); _monitor.Failed += TransferFailed; Control1.DataContext = _monitor; } }
private void BeginDownload() { using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { if (storage.FileExists("/shared/transfers/channel9.mp3")) storage.DeleteFile("/shared/transfers/channel9.mp3"); } _request = new BackgroundTransferRequest( new Uri("http://media.ch9.ms/ch9/7e13/ce6ea97c-e233-4e7c-a74d-ee1c81e37e13/WP8JumpStart04.mp3", UriKind.Absolute), new Uri("/shared/transfers/channel9.mp3", UriKind.Relative)) { Tag = "Channel9Download", TransferPreferences = TransferPreferences.AllowBattery }; _monitor = new TransferMonitor(_request); Control1.DataContext = _monitor; _monitor.RequestStart(); //This adds request to the BackgroundTransferService queue. }
private void RestartDownload() { try { BackgroundTransferService.Remove(_request); } catch (Exception) { //No need to worry. These errors can be surpressed. We are just making sure the download has been completed cancelled. } _request = null; _monitor = null; BeginDownload(); }
public BackgroundTransferQueue() { InitializeComponent(); // Load existing background requests into the collection foreach (BackgroundTransferRequest request in BackgroundTransferService.Requests) { var monitor = new TransferMonitor(request); SubscribeMonitor(monitor); _list.Add(monitor); } TransfersList.DataContext = _list; // Subscribe buttons to actions _selectButton.Click += OnSelectButtonClick; _addButton.Click += OnAddButtonClick; _cancelButton.Click += OnCancelButtonClick; _list.CollectionChanged += delegate { _selectButton.IsEnabled = (_list.Count > 0); }; ReloadApplicationBar(); }
private TransferMonitorViewModel(TransferMonitor monitor) { Monitor = monitor; }
private void UnsubscribeMonitor(TransferMonitor monitor) { monitor.Failed -= TransferCanceled; }
public void SubscribeMonitor(TransferMonitor monitor) { UnsubscribeMonitor(monitor); // prevent duplicate subscription monitor.Failed += TransferCanceled; }
/// <summary> /// This function is for demo purposes. It adds three files to the background download queue and displays them in the multi-select list. /// </summary> private void OnAddButtonClick(object sender, EventArgs e) { Dictionary<string, Uri> urlPresets = new Dictionary<string, Uri> { {"21 MB File",new Uri("http://media.ch9.ms/ch9/ecbc/cfcb0ad7-fbdd-47b0-aabf-4da5e3e0ecbc/WP8JumpStart06.mp3",UriKind.Absolute)}, {"34 MB File",new Uri("http://media.ch9.ms/ch9/7e13/ce6ea97c-e233-4e7c-a74d-ee1c81e37e13/WP8JumpStart04.mp3",UriKind.Absolute)}, {"92 MB File",new Uri("http://media.ch9.ms/ch9/7e13/ce6ea97c-e233-4e7c-a74d-ee1c81e37e13/WP8JumpStart04.wmv",UriKind.Absolute)}, }; foreach (var preset in urlPresets) { Uri saveLocation = new Uri("/shared/transfers/" + preset.Key, UriKind.Relative); BackgroundTransferRequest request = new BackgroundTransferRequest(preset.Value, saveLocation) { TransferPreferences = TransferPreferences.AllowBattery // Note: this will not use cellular data to download }; TransferMonitor monitor = new TransferMonitor(request, preset.Key); try { BackgroundTransferService.Add(request); } catch (Exception err) // An exception is thrown if this transfer is already requested. { Debug.WriteLine(err); continue; } monitor.Failed += TransferCanceled; _list.Add(monitor); } }