【问题标题】:How can I remove an item from a list?如何从列表中删除项目?
【发布时间】:2016-03-12 20:06:45
【问题描述】:

我是 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%->31%->32%(by processbar)
  2. item2 等待 0%
  3. item3 等待 0%

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) 你想做什么 2) 你做了什么 3) 为什么它不起作用?
  • 所以,列表框有数据,然后我单击它上面的一个项目,然后单击按钮。我想从列表框的来源(var Streamitem = LsvTrackSoundCloud.SelectedItem as Model.TracksSoundCloud.Track;)提供三个参数到私有异步无效下载(字符串 requestUrl,字符串文件名,字符串文件类型),我还想绑定名称,状态,值(processbar 的值)到其他列表框。

标签: c# wpf mvvm


【解决方案1】:

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

【讨论】:

  • 对不起,我是 mvvm 的拳头,所以我不明白你在说什么。你会写代码吗?
  • 你有没有我可以查看的精简版工作代码
猜你喜欢
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 2022-12-20
  • 2017-08-31
  • 2020-02-16
  • 2018-09-27
相关资源
最近更新 更多