简体   繁体   English

Windows Store应用程序-在下载类中使用进度栏

[英]Windows Store App - Use Progress Bar in a Download Class

I have a ProgressBar in my MainPage.xaml: 我的MainPage.xaml中有一个ProgressBar:

<ProgressBar HorizontalAlignment="Center"
                 Height="90" Margin="0,-30,0,0" 
                 VerticalAlignment="Center" 
                 Width="600" x:Name="BarProgress" /> 

And then in MainPage.xaml.cs there is the code for downloading data: 然后在MainPage.xaml.cs中有用于下载数据的代码:

public async Task Download(string fileName, string fileId)
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
private async Task DiscoverActiveDownloadsAsync()
private void DownloadProgress(DownloadOperation download)
        {            
            double percent = 100;
            if (download.Progress.TotalBytesToReceive > 0)
            {
                percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;
            }

            BarProgress.Value = percent;                        
        }

And it works great! 而且效果很好! I can see the ProgressBar proceeds until the download ends! 我可以看到ProgressBar继续进行,直到下载结束!

But if i want to create a new class such as DownloadData.xaml.cs and then put inside all the code for downloading data, how can i invoke the BarProgress.Value?!? 但是,如果我想创建一个新类,例如DownloadData.xaml.cs,然后将所有用于下载数据的代码放入其中,该如何调用BarProgress.Value ?! I've done a lot of tentatives, such as MainPage.BarProgress.Value = percent, but i receive a lot of errors because BarProgress is inaccesible... 我做了很多尝试,例如MainPage.BarProgress.Value = percent,但是我收到很多错误,因为BarProgress无法进行...

I would probably move the Progressbar over to a DownloadData.xaml usercontrol. 我可能会将Progressbar移到DownloadData.xaml用户控件。 That way, every time you have download data, it has it's own progressbar that can be accessed from the code behind. 这样,每次下载数据时,它都有自己的进度条,可以从后面的代码中访问它。 If you really don't want to do that, another way might be to a search for the control and try and grab it that way. 如果您确实不想这样做,则另一种方法可能是搜索控件并尝试以这种方式获取它。 Some useful tips for that are here: Find controls . 此处提供了一些有用的提示: 查找控件 Edit: Create a subfolder to your project for UserControls Right-click on your project and add User Control. 编辑:为用户控件的项目创建一个子文件夹右键单击您的项目并添加用户控件。 This will add a xaml page and a xaml.cs code behind page. 这将在页面后面添加一个xaml页面和一个xaml.cs代码。 Next you want to add the control as explained here . 接下来,您要按照此处的说明添加控件。 That should do it. 那应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM