コード例 #1
0
        /// <summary>
        /// Process the download events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void WatchItems_OnDownloadEvent(object sender, WatchItems.WatchItems.DownloadEventArgs e)
        {
            switch (e.State)
            {
            case WatchItems.WatchItems.DownloadState.Initiate:

                grDownload.Visibility = Visibility.Visible;
                btnCancel.Visibility  = Visibility.Collapsed;
                btnStop.Visibility    = Visibility.Visible;

                break;

            case WatchItems.WatchItems.DownloadState.InProgress:

                pbDownload.Value = int.Parse(e.Status);

                break;

            case WatchItems.WatchItems.DownloadState.Done:

                txtDownload.Text     = String.Format("Download completed");
                btnCancel.Visibility = Visibility.Visible;
                btnStop.Visibility   = Visibility.Collapsed;

                //Set the name of the file in a localsetting
                var localSettings = ApplicationData.Current.LocalSettings;
                localSettings.Values[Constants.BackgroundCommunicatieDownloadedItem] = e.Status;

                //Start background task to sed new item to pebble
                Pebble_Time_Manager.Connector.PebbleConnector _pc = Pebble_Time_Manager.Connector.PebbleConnector.GetInstance();
                try
                {
                    await _pc.StartBackgroundTask(Connector.PebbleConnector.Initiator.AddItem);
                }
                catch (Exception exp)
                {
                    System.Diagnostics.Debug.WriteLine(exp.Message);
                }

                //Add new item to viewmodel
                WatchItems.WatchItem _newItem;
                _newItem = await WatchItems.WatchItem.Load(e.Status);

                await _pc.WatchItems.AddWatchItem((WatchItems.WatchItem) _newItem);

                break;

            case WatchItems.WatchItems.DownloadState.Error:

                MessageDialog msgBox = new MessageDialog("An error occurred while downloading the requested item.", "Error");
                msgBox.ShowAsync();

                break;

            case WatchItems.WatchItems.DownloadState.Canceled:

                grDownload.Visibility = Visibility.Collapsed;
                btnCancel.Visibility  = Visibility.Collapsed;
                btnStop.Visibility    = Visibility.Visible;

                break;
            }
        }
コード例 #2
0
        private static async Task HandleDownloadAsync(DownloadOperation download, bool start)
        {
            try
            {
                System.Diagnostics.Debug.WriteLine("Running: " + download.Guid);

                // Store the download so we can pause/resume.
                Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress);
                if (start)
                {
                    // Start the download and attach a progress handler.
                    await download.StartAsync().AsTask(cts.Token, progressCallback);
                }
                else
                {
                    // The download was already running when the application started, re-attach the progress handler.
                    await download.AttachAsync().AsTask(cts.Token, progressCallback);
                }

                ResponseInformation response = download.GetResponseInformation();

                System.Diagnostics.Debug.WriteLine(String.Format("Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode));

                if (response.StatusCode == 200)
                {
                    System.Diagnostics.Debug.WriteLine("Downloaded file: " + download.ResultFile.Name);

                    OnDownloadEvent?.Invoke(null, new DownloadEventArgs(download.ResultFile.Name, DownloadState.Done, newWatchItem));

                    //Set the name of the file in a localsetting
                    var localSettings = ApplicationData.Current.LocalSettings;
                    localSettings.Values[Constants.BackgroundCommunicatieDownloadedItem] = download.ResultFile.Name;

                    //Start background task to sed new item to pebble
                    Pebble_Time_Manager.Connector.PebbleConnector _pc = Pebble_Time_Manager.Connector.PebbleConnector.GetInstance();
                    try
                    {
                        await _pc.StartBackgroundTask(Connector.PebbleConnector.Initiator.AddItem);
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e.Message);
                    }

                    //Add new item to viewmodel
                    WatchItem _newItem;
                    _newItem = await WatchItem.Load(download.ResultFile.Name);

                    await _pc.WatchItems.AddWatchItem((WatchItem)_newItem);

                    cts = null;
                }
                else
                {
                    OnDownloadEvent?.Invoke(null, new DownloadEventArgs("An error occurred while downloading", DownloadState.Error));
                    cts = null;
                }
            }
            catch (TaskCanceledException)
            {
                System.Diagnostics.Debug.WriteLine("Canceled: " + download.Guid);
                OnDownloadEvent?.Invoke(null, new DownloadEventArgs(download.ResultFile.Name, DownloadState.Canceled));
                cts = null;
            }
        }