-
Notifications
You must be signed in to change notification settings - Fork 68
/
MainPage.xaml.cs
112 lines (95 loc) · 4.37 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using DownloadExample;
using Plugin.DownloadManager;
using Plugin.DownloadManager.Abstractions;
using System;
using System.IO;
using System.Reflection;
using Windows.Networking.BackgroundTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace UWP
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
BackgroundDownloader backgroundDownloader = new BackgroundDownloader();
string path;
Downloader foo;
public MainPage()
{
this.InitializeComponent();
//FolderPicker folderPicker = new FolderPicker();
//folderPicker.SuggestedStartLocation = PickerLocationId.Downloads;
//folderPicker.ViewMode = PickerViewMode.Thumbnail;
//folderPicker.FileTypeFilter.Add("*");
//StorageFolder folder = await folderPicker.PickSingleFolderAsync();
// TODO: Find a way to set a custom destination ... I always got a permission-denied-exception ...
// Maybe possible in combination with https://msdn.microsoft.com/library/windows/apps/br207457
// See: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions
//CrossDownloadManager.Current.PathNameForDownloadedFile = new Func<IDownloadFile, string> (file => path);
foo = new Downloader();
}
public void Download()
{
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
path = Path.Combine(folder.Path, "data_10mb.test");
// If already downloading, abort it.
if (foo.IsDownloading())
{
foo.AbortDownloading();
downloadBtn.Content = "Download aborted.";
return;
}
downloadBtn.Content = "Start downloading ...";
foo.InitializeDownload();
foo.File.PropertyChanged += (sender, e) => {
System.Diagnostics.Debug.WriteLine("[Property changed] " + e.PropertyName + " -> " + sender.GetType().GetProperty(e.PropertyName).GetValue(sender, null).ToString());
// Update UI text-fields
var downloadFile = ((IDownloadFile)sender);
switch (e.PropertyName)
{
case nameof(IDownloadFile.Status):
Statustext.Text = downloadFile.Status.ToString();
break;
case nameof(IDownloadFile.TotalBytesExpected):
BytesExpected.Text = downloadFile.TotalBytesExpected.ToString();
break;
case nameof(IDownloadFile.TotalBytesWritten):
BytesReceived.Text = downloadFile.TotalBytesWritten.ToString();
break;
}
// Update UI if download-status changed.
if (e.PropertyName == "Status")
{
switch (((IDownloadFile)sender).Status)
{
case DownloadFileStatus.COMPLETED:
case DownloadFileStatus.FAILED:
case DownloadFileStatus.CANCELED:
downloadBtn.Content = "Downloading finished.";
((DownloadFileImplementation)downloadFile).DownloadOperation.ResultFile.DeleteAsync();
break;
}
}
// Update UI while donwloading.
if (e.PropertyName == "TotalBytesWritten" || e.PropertyName == "TotalBytesExpected")
{
var bytesExpected = ((IDownloadFile)sender).TotalBytesExpected;
var bytesWritten = ((IDownloadFile)sender).TotalBytesWritten;
if (bytesExpected > 0)
{
var percentage = Math.Round(bytesWritten / bytesExpected * 100);
downloadBtn.Content = "Downloading (" + percentage + "%)";
}
}
};
foo.StartDownloading(CellularNetworkAllowed.IsChecked.HasValue && CellularNetworkAllowed.IsChecked.Value);
}
private void downloadBtn_Click(object sender, RoutedEventArgs e)
{
Download();
}
}
}