如何从列表中删除项目

本文关键字:删除项目 列表 | 更新日期: 2023-09-27 18:26:06

我是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…)。例如:

我有三个项目。

  1. 项目1下载30%->31%->32%(通过processbar)
  2. 项目2正在等待0%
  3. 项目3正在等待0%

项目1完成并删除后,项目2将下载。。。。

<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.
            }

如何从列表中删除项目

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