背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
2021-04-03 00:28
标签:进度 getc lock blog style load pat data catch [源码下载] BackgroundTask/TransferGroup.xaml BackgroundTask/TransferGroup.xaml.cs 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知) 标签:进度 getc lock blog style load pat data catch 原文地址:https://www.cnblogs.com/webabcd/p/9211752.html
作者:webabcd
介绍
背水一战 Windows 10 之 后台任务
示例
演示后台下载任务的分组,以及如何设置组内任务是并行执行还是串行执行,以及组任务全部完成后如何 toast 或 tile 通知)
BackgroundTask/TransferModel.cs/*
* 扩展了 DownloadOperation 和 UploadOperation,用于 MVVM 绑定数据
*/
using System;
using System.ComponentModel;
using Windows.Networking.BackgroundTransfer;
namespace Windows10.BackgroundTask
{
public class TransferModel : INotifyPropertyChanged
{
public DownloadOperation DownloadOperation { get; set; }
public UploadOperation UploadOperation { get; set; }
public string Source { get; set; }
public string Destination { get; set; }
private string _progress;
public string Progress
{
get { return _progress; }
set
{
_progress = value;
RaisePropertyChanged("Progress");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
Page
x:Class="Windows10.BackgroundTask.TransferGroup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.BackgroundTask"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
ScrollViewer Name="scrollViewer" Height="100" Margin="5">
TextBlock Name="lblMsg" TextWrapping="Wrap" />
ScrollViewer>
Button Name="btnAddDownload" Content="新增一组(3 个)下载任务,可以指定其是并发还是串行,当这一组的所有任务都完成后弹出通知" Margin="5" Click="btnAddDownload_Click" />
Button Name="btnCancel" Content="取消所有下载任务" Margin="5" Click="btnCancel_Click" />
ListView Name="listView" Height="286" Padding="5">
ListView.ItemTemplate>
DataTemplate>
StackPanel Margin="0 5" Background="Blue">
TextBlock Text="{Binding Source}" Margin="5" />
TextBlock Text="{Binding Destination}" Margin="5" />
TextBlock Text="{Binding Progress}" Margin="5" />
StackPanel>
DataTemplate>
ListView.ItemTemplate>
ListView>
StackPanel>
Grid>
Page>
/*
* 演示后台下载任务的分组,以及如何设置组内任务是并行执行还是串行执行,以及组任务全部完成后如何 toast 或 tile 通知)
*
* BackgroundTransferGroup - 后台下载任务的分组对象
* static BackgroundTransferGroup CreateGroup(string name) - 创建指定分组标识的 BackgroundTransferGroup 对象
* Name - 分组标识(只读)
* TransferBehavior - 组内下载任务的执行方式,BackgroundTransferBehavior 枚举
* Parallel - 并行
* Serialized - 串行
*
* BackgroundDownloader - 后台下载任务管理器
* TransferGroup - 设置或获取分组对象(BackgroundTransferGroup 类型)
* static GetCurrentDownloadsForTransferGroupAsync(BackgroundTransferGroup group) - 获取指定组的所有下载任务
*
* DownloadOperation - 下载任务对象
* TransferGroup - 获取此下载任务的分组对象(BackgroundTransferGroup 类型)
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
namespace Windows10.BackgroundTask
{
public sealed partial class TransferGroup : Page
{
// 用于后台任务的分组(通过组名标识后台任务)
private BackgroundTransferGroup _group = BackgroundTransferGroup.CreateGroup("my_group");
// 下载任务的集合
private ObservableCollection
OK
[源码下载]
文章标题:背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
文章链接:http://soscw.com/index.php/essay/71614.html