0

I have a ProgressBar in my MainPage.xaml:

<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:

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!

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?!? I've done a lot of tentatives, such as MainPage.BarProgress.Value = percent, but i receive a lot of errors because BarProgress is inaccesible...

CaptainAmerica
  • 83
  • 1
  • 15
  • Since it has a protection level error, could you share more DownloadData class code? More specifically the class and method signatures? – Danexxtone Sep 23 '13 at 14:23
  • i've modified a little bit the question with my last understanding! – CaptainAmerica Sep 27 '13 at 14:29
  • Are they thread related errors or protection level errors? If they are protection level, you may want to move the progressbar over to downloaddata.xaml. And I'm just thinking out loud here. – Danexxtone Sep 27 '13 at 15:35
  • Sorry but i've not a DownloadData.xaml...i have a simple class that is responsible for download DownloadData.cs! – CaptainAmerica Sep 28 '13 at 13:47

1 Answers1

0

I would probably move the Progressbar over to a DownloadData.xaml usercontrol. 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. Next you want to add the control as explained here. That should do it.

Community
  • 1
  • 1
Danexxtone
  • 773
  • 4
  • 11
  • Nice idea having a DownloadData.xaml...how can i nested this into the MainPage.xaml? – CaptainAmerica Oct 01 '13 at 08:07
  • Thank you for your help Danexxtone!It seems to be the best approach so far!I was able to create the UserControl with the ProgressBar inside and import in my Main XAML, but if i put BarProgress.Value = 2 in the constructor of the UserControl it works great but if i would update the value during the Download the ProgressBar.Value doesn't refresh...this is a problem since we have adopted this solution as a workaround for this!!!!!Thank you in advance! – CaptainAmerica Oct 01 '13 at 16:11
  • At the moment the Task isn't updating the UI thread and we have to account for that. You can either search for that issue (there's quite a few solutions out there) or show me the code for DownloadData.xaml and xaml.cs and we'll see if we can update your code. – Danexxtone Oct 01 '13 at 16:24
  • What i've done is simply copying the code in my question for downloading data from MainPage.xaml.cs to DownloadControl.xaml.cs. Then in MainPage in LoadState i invoke the entry point for downloading:DownloadControl dwCont = new DownloaControl(); dwCont=.downloadDocumentData(rootObj); In the XAML i have the same ProgressBar of the question...It seems an easy thing but really it doesn't!!!!! – CaptainAmerica Oct 01 '13 at 16:56
  • You should be able to follow these steps and have an awesome progress bar by the end of it: http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx – Danexxtone Oct 01 '13 at 17:46