private async void DownloaderStateChanged(Enums.DownloadState state)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                Debug.WriteLine("State Change: " + state.ToString());
                VisualStateManager.GoToState(this, state.ToString(), false);
                if (state == Enums.DownloadState.Done)
                {
                    SizeBlock.Text = StringConverter.GetPrintSize(downloader.Message.DownloadSize);

                    string folderPath = (await StorageManager.TryGetFolderAsync(downloader.Message.FolderToken))?.Path;
                    if (folderPath == null)
                    {
                        folderPath = Strings.AppResources.GetString("FolderNotExist");
                    }
                    SpeedBlock.Text = folderPath;

                    Models.DownloaderMessage message = downloader.Message;
                    NameBlock.Text = message.FileName + message.Extention;
                }
                else if (state == Enums.DownloadState.Prepared)
                {
                    Models.DownloaderMessage message = downloader.Message;
                    NameBlock.Text = message.FileName + message.Extention;
                    int per        = (int)((message.FileSize == null) ? 0
                        : (100f * message.DownloadSize / message.FileSize));
                    ProgressBlock.Text = (message.FileSize == null) ? "-%" : (per + "%");
                    Bar.Value          = per;
                    SizeBlock.Text     = StringConverter.GetPrintSize(message.DownloadSize)
                                         + " / " + (message.FileSize == null ? "--" :
                                                    StringConverter.GetPrintSize((long)message.FileSize));
                    SpeedBlock.Text = "-/s ";
                }
            });
        }
Beispiel #2
0
 private async void DownloadCompleted(Models.DownloaderMessage message)
 {
     //必须Dispose(), 否则里面的Timer不会停止
     downloader.Dispose();
     await MainPage.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                                () => { MainPage.Current.DownloadBarCollection.Remove(this); });
 }
        private async void DownloadCompleted(Models.DownloaderMessage message)
        {
            var folder = await StorageManager.TryGetFolderAsync(message.FolderToken);

            //播放一个通知
            if (Settings.IsNotificationShownWhenTaskCompleted)
            {
                Toasts.ToastManager.ShowDownloadCompleteToastAsync(Strings.AppResources.GetString("DownloadCompleted"), message.FileName + " - " +
                                                                   Converters.StringConverter.GetPrintSize((long)message.FileSize),
                                                                   Path.Combine(folder.Path, message.FileName + message.Extention),
                                                                   folder.Path);
            }

            await MainPage.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                                       () => {
                MainPage.Current.DownloadBarManager.Invoke(
                    (collection) =>
                {
                    int completed = collection.Count - 1;
                    for (int i = collection.Count - 1; i >= 0; --i)
                    {
                        if (collection[i].downloader.Message.IsDone)
                        {
                            if (completed == i)
                            {
                                --completed;
                                continue;
                            }
                            else
                            {
                                collection.Move(i, completed);
                                --completed;
                                ++i;
                            }
                        }
                    }
                }
                    );
            });
        }
        /// <summary>
        /// 从url获取网络信息
        /// </summary>
        public static async Task <Models.DownloaderMessage> GetMessageAsync(string url)
        {
            Models.DownloaderMessage message = new Models.DownloaderMessage();

            try
            {
                HttpWebRequest  req = WebRequest.CreateHttp(url);
                HttpWebResponse res = (HttpWebResponse)(await req.GetResponseAsync());

                message.URL = url;

                //文件全名 XXX.XXX
                string fullName = GetResponseName(res);

                message.FileName  = System.IO.Path.GetFileNameWithoutExtension(fullName);
                message.Extention = System.IO.Path.GetExtension(fullName);
                message.FileSize  = GetResponseSize(res);

                if (res != null)
                {
                    res.Dispose();
                    res = null;
                }

                if (req != null)
                {
                    req.Abort();
                    req = null;
                }

                GC.Collect();
                return(message);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                throw e;
            }
        }
Beispiel #5
0
        private async void DownloaderStateChanged(Enums.DownloadState state)
        {
            //在后台运行(挂起或最小化)不更新UI
            if (((App)App.Current).InBackground)
            {
                return;
            }
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                if (state == Enums.DownloadState.Pause)
                {
                    PauseButton.IsEnabled   = false;
                    PlayButton.IsEnabled    = true;
                    DeleteButton.IsEnabled  = true;
                    RefreshButton.IsEnabled = true;
                    Bar.ShowPaused          = true;
                }
                else if (state == Enums.DownloadState.Error)
                {
                    PauseButton.IsEnabled   = false;
                    PlayButton.IsEnabled    = false;
                    DeleteButton.IsEnabled  = true;
                    RefreshButton.IsEnabled = true;
                    Bar.ShowPaused          = true;
                }
                else if (state == Enums.DownloadState.Downloading)
                {
                    PauseButton.IsEnabled   = true;
                    PlayButton.IsEnabled    = false;
                    DeleteButton.IsEnabled  = true;
                    RefreshButton.IsEnabled = true;
                    Bar.ShowPaused          = false;
                }
                else if (state == Enums.DownloadState.Done)
                {
                    Bar.Value               = 100;
                    ProgressBlock.Text      = "100%";
                    PlayButton.IsEnabled    = false;
                    PauseButton.IsEnabled   = false;
                    DeleteButton.IsEnabled  = false;
                    RefreshButton.IsEnabled = false;
                }
                else if (state == Enums.DownloadState.Prepared)
                {
                    HideGlassLabel.Begin();

                    Models.DownloaderMessage message = downloader.Message;
                    NameBlock.Text          = message.FileName + message.Extention;
                    PauseButton.IsEnabled   = false;
                    PlayButton.IsEnabled    = true;
                    DeleteButton.IsEnabled  = true;
                    RefreshButton.IsEnabled = false;
                    int per = (int)((message.FileSize == null) ? 0
                        : (100f * message.DownloadSize / message.FileSize));
                    ProgressBlock.Text = (message.FileSize == null) ? "-%" : (per + "%");
                    Bar.Value          = per;
                    SizeBlock.Text     = StringConverter.GetPrintSize(message.DownloadSize)
                                         + " / " + (message.FileSize == null ? "--" :
                                                    StringConverter.GetPrintSize((long)message.FileSize));
                    SpeedBlock.Text = "-/s ";
                }
            });
        }