如何从列表中删除项目?

时间:2015-12-08 02:14:10

标签: c# wpf mvvm

我是mvvm的新手: 正常方式:

double progress;
    private DownloadOperation _activeDownload;
    private async void ProgressCallback(DownloadOperation obj)
    {
        try
        {
            progress
            = ((double)obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive);
            processDownload.Value = progress * 100;
            double _bytesToMBreceived = (double)(obj.Progress.BytesReceived / 1024) / 1024;
            double _bytesToMBtotal = (double)(obj.Progress.TotalBytesToReceive / 1024) / 1024;
            buffering.Text = "Downloading..." + string.Format("{0:0.00}", _bytesToMBreceived) + "MB /" + string.Format("{0:0.00}", _bytesToMBtotal) + "MB";
            if (progress >= 1.0)
            {
                _activeDownload = null;
                processDownload.Value = 0;
                buffering.Text = "Completed...";
                await Task.Delay(500);
                gridDownload.Visibility = Visibility.Collapsed;

                var dialog = new MessageDialog("Download has completed");
                await dialog.ShowAsync();
            }
        }
        catch (Exception)
        {
            return;
        }

    }
    private async Task StartDownloadAsync(DownloadOperation downloadoperation)
    {

        gridDownload.Visibility = Visibility.Visible;
        _activeDownload = downloadoperation;
        buffering.Text = "Waiting for a minute...";
        processDownload.Visibility = Visibility.Visible;
        var process = new Progress<DownloadOperation>(ProgressCallback);
        await downloadoperation.StartAsync().AsTask(process);
    }
    private async void downloadSongs(string requestUrl, string filename)
    {
        try
        {
            if (filename == null)
                throw new ArgumentNullException("filename");
            //
            var downloader = new BackgroundDownloader();
            //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename + ".mp3", CreationCollisionOption.ReplaceExisting);
            var regex = new Regex(@"[\\|/\:\*\?""<>\\|]");
            var result_filename = regex.Replace(filename, " ").Replace(";", "");


            FolderPicker fo = new FolderPicker();
            fo.SuggestedStartLocation = PickerLocationId.Downloads;
            fo.FileTypeFilter.Add(".mp3");
            StorageFolder folder = await fo.PickSingleFolderAsync();
            var filePart = await folder.CreateFileAsync(result_filename + ".mp3", CreationCollisionOption.GenerateUniqueName);
            DownloadOperation download = downloader.CreateDownload(new Uri(requestUrl), filePart);
            await StartDownloadAsync(download);
        }

        catch (Exception)
        {
            return;
        }
    }

    private async void btnDownload_Tapped(object sender, TappedRoutedEventArgs e)
    {
        try
        {
            if (buffering.Text == "Waiting for a minute..." && processDownload.Value == 0 || processDownload.Value != 0)
            {
                throw new Exception("You must waiting for downloading completed to download again");
            }
            else
                downloadSongs(_linkdownload, _linktitle);
        }

        catch (Exception ex)
        {
            var Dialog = new MessageDialog(ex.Message);
            await Dialog.ShowAsync();
        }
    }

我想将此代码切换为mvvm方式(不是轻量级mvvm ...)。 例如:

我有3个项目。

  1. item1下载30% - &gt; 31% - &gt; 32%(按流程栏)
  2. item2等待0%
  3. item3等待0%
  4. 在item1完成并删除后,item2将下载....

    <listbox.....>
       <listbox.Itemtemplate>
          <textblock text={binding title}
          <textblock text{binding status}
          <processbar value={binding process}
    ........
    

    MVVM方式:

    public class DownloadCommand
    {
        public string name { get; set; }
        public string status { get; set; }
        public int value { get; set; }
        public ICommand _downloadCommand { get; set; }
    
        public DownloadCommand()
        {
            _downloadCommand = new DownloadButtonClick();
        }
    
    
    
    
        public void ProgressCallBack(DownloadOperation obj)
        {
            double progress = ((double)obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive);
            double _bytesReceived = (double)(obj.Progress.BytesReceived / 1240);
            double _bytesTotal = (double)(obj.Progress.TotalBytesToReceive / 1240);
            if (progress != 1.0)
            {
                //listbox will removed this item and keep to download at next item.
            }
        }
    
        private async Task StartDownloadAsync(DownloadOperation obj)
        {
            var process = new Progress<DownloadOperation>(ProgressCallBack);
            await obj.StartAsync().AsTask(process);
        }
    
        private async void Download(string requestUrl, string filename, string fileType)
        {
            var downloader = new BackgroundDownloader();
            var regex = new Regex(@"[\\|/\:\*\?""<>\\|]");
            var result_filename = regex.Replace(filename, " ").Replace(";", "");
    
            FolderPicker fo = new FolderPicker();
            fo.SuggestedStartLocation = PickerLocationId.Downloads;
            fo.FileTypeFilter.Add(fileType);
            StorageFolder folder = await fo.PickSingleFolderAsync();
            var filePart = await folder.CreateFileAsync(result_filename + fileType, CreationCollisionOption.ReplaceExisting);
            DownloadOperation download = downloader.CreateDownload(new Uri(requestUrl), filePart);
            await StartDownloadAsync(download);
        }
    
        public class DownloadButtonClick : ICommand
        {
            public event EventHandler CanExecuteChanged;
    
            public bool CanExecute(object parameter)
            {
                return true;
            }
    
            public void Execute(object parameter)
            {
                //throw new NotImplementedException();
            }
        }
    

    我不知道在这里写什么。

        public void Execute(object parameter)
                {
                    //throw new NotImplementedException();
                }
    
    if (progress != 1.0)
                {
                    //listbox will removed this item and keep to download at next item.
                }
    

1 个答案:

答案 0 :(得分:0)

执行操作的入口点。例如,如果您将命令与按钮相关联,则在按钮单击时将触发此执行。该参数是你在XAML中提到的命令参数。从那里你可以调用你的StartDownloadAsync()。如果您有正确的数据绑定进度将反映在UI中。如果您有任何剥离的工作样本,那将会有所帮助。