我正在尝试在C#中为UWP创建一个下载管理器。我需要添加ProgressBar用于下载进度,TextBlock用于显示下载状态,另一个TextBox用于显示下载文件名。当我单独编译下载管理器的代码时(在一个新的uwp应用程序上),我的代码工作正常。它在运行时正确显示进度。 我的代码是 -
private async void Downloadbtn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Uri url = new Uri(urlBox.Text);
string fileName = ResolveName(urlBox.Text);
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.List;
folderPicker.FileTypeFilter.Add("*.*");
StorageFolder downloadfolder = await folderPicker.PickSingleFolderAsync();
if (folderPicker != null)
{
StorageFile downloadFile = await downloadfolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
operation = downloader.CreateDownload(url, downloadFile);
try
{
TextBlock fileNameText = new TextBlock();
fileNameText.Text = fileName;
ProgressBar ProgressIndicator = new ProgressBar();
ProgressIndicator.Name = "progress";
TextBlock progressText = new TextBlock();
progressText.Name = "progressText";
StackPanel panel = new StackPanel();
panel.Children.Add(fileNameText);
panel.Children.Add(ProgressIndicator);
panel.Children.Add(progressText);
MainStack.Children.Add(panel);
cts = new CancellationTokenSource();
Progress<DownloadOperation> progressCallBack = new Progress<DownloadOperation>(Progress);
await operation.StartAsync().AsTask(cts.Token,progressCallBack);
}
catch (Exception)
{
await operation.ResultFile.DeleteAsync();
operation = null;
}
}
}
private string ResolveName(string fileName)
{
if (fileName.Contains("/"))
{
int last = fileName.LastIndexOf("/");
fileName = fileName.Substring(last + 1, fileName.Length - last - 1);
}
if (fileName.Contains("%20"))
{
fileName = fileName.Replace("%20", " ");
}
return fileName;
}
private void Progress(DownloadOperation operation)
{
ProgressBar c = new ProgressBar();
c = (ProgressBar)(MainStack.FindName("progress" ));
TextBlock c1 = new TextBlock();
c1 = (TextBlock)(MainStack.FindName("progressText"));
double received = operation.Progress.BytesReceived;
double toReceive = operation.Progress.TotalBytesToReceive;
double progress = received * 100 / toReceive;
((ProgressBar)c).Value = progress;
c1.Text = received + "KB received of " + toReceive + "KB";
if (received == toReceive)
c1.Text = "Download Completed";
}
MainStack是添加新下载及其ui元素的stackpanel。
但当我将上述代码放在不同的应用中时,UI元素(进度条,文本块)不可见 -
private async void GetDownloadData(Uri uri)
{
string fileName = ResolveName(uri.tostring());
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.List;
folderPicker.FileTypeFilter.Add("*.*");
StorageFolder downloadfolder = await folderPicker.PickSingleFolderAsync();
if (folderPicker != null)
{
StorageFile downloadFile = await downloadfolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
operation = downloader.CreateDownload(url, downloadFile);
try
{
TextBlock fileNameText = new TextBlock();
fileNameText.Text = fileName;
ProgressBar ProgressIndicator = new ProgressBar();
ProgressIndicator.Name = "progress" ;
TextBlock progressText = new TextBlock();
progressText.Name = "progressText";
StackPanel panel = new StackPanel();
panel.Children.Add(fileNameText);
panel.Children.Add(ProgressIndicator);
panel.Children.Add(progressText);
MainStack.Children.Add(panel);
cts = new CancellationTokenSource();
Progress<DownloadOperation> progressCallBack = new Progress<DownloadOperation>(Progress);
await operation.StartAsync().AsTask(cts.Token,progressCallBack);
}
catch (Exception)
{
await operation.ResultFile.DeleteAsync();
operation = null;
}
}
}
在第二个代码中,我调用GetDownloadData(uri)方法来启动下载,而不是在按钮单击事件时调用它。此方法也放在不同的XAML页面(主页除外)。
第二个代码的唯一问题是,在MainStack(stackpanel)中运行时没有添加进度条或文本块。我尝试通过用listview和listbox替换stackpanel来解决这个问题,但问题无法解决。 任何想法,我在这里缺少什么。