Skip to main content

Microsoft Silverlight

Answered Question How can I refresh the UI view of a user control from a silverlight classRSS Feed

(0)

djcohen66
djcohen66

Member

Member

0 points

3 Posts

How can I refresh the UI view of a user control from a silverlight class

I have build a small sliverlight app that attempts to do client side validation of a delimited text file (comparing to a file definition) before allowing the user to upload the file to our servers for further processing.  I am using an OpenFileDialog to open and read the file.  It was my hope to display a progress bar to show progress in validating the files, however the screen doesn't update visually until the reading of the line by line loop though the StreamReader is completed.

 
            using (StreamReader reader = new StreamReader(openFileDialog1.File.OpenRead()))
            {
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        l_lngLineCount++;
                       //Count the lines and add each line to a dictionary for validation later so that I can set the percentage of the progress bar.
                       dicDataStore.Add(l_lngLineCount, line);
                    }
                }
            }
  
Can I force the screen to refresh after the user selects their file in the dialog?

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP

Re: How can I refresh the UI view of a user control from a silverlight class

How are you updating the UI? Is there a variable that is being used?

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

djcohen66
djcohen66

Member

Member

0 points

3 Posts

Re: How can I refresh the UI view of a user control from a silverlight class

Below is the full function.  I am not 100% sure what you are asking me.  There is a progress bar and I am setting the percent complete as it changes.  There is a nested CheckLine function that sets the pct variable =

decimal.Round((((decimal)l_lngCurLineNumber / (decimal)l_lngLineCount) * 100), 0)

But I get the feeling you are talking about a user control variable which would pretty much just be my main page.

        void OpenAndValidate(List aryDefList, Ellipse BlackLight, Ellipse GreenLight, Ellipse RedLight, ProgressBar pb)
        {
            tbResult.Text = "";
            bool bResult = false;
            l_bFileValidated = true;
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            l_lngLineCount = 0;
            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();
            if (openFileDialog1.File == null)
            {
                return;
            }
            
            tbResult.Text += "\n";
            tbResult.Text += "Reading File: " + openFileDialog1.File.ToString();
            StreamReader reader = openFileDialog1.File.OpenText();
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                l_lngLineCount++;
                dicDataStore.Add(l_lngLineCount, line);
            }
            reader.Close();
            reader.Dispose();
            BlackLight.Visibility = Visibility.Collapsed;
            GreenLight.Visibility = Visibility.Visible;
            bResult = true;
            if (dicDataStore.Count > 0)
            {
                foreach (KeyValuePair<long, string> kvp in dicDataStore)
                {
                    //retVal.AppendLine("Line " + kvp.Key.ToString() + ": " + kvp.Value.ToString());
                    l_lngCurLineNumber++;
                    if (l_lngCurLineNumber != 1)
                    {
                        if (!CheckLine(kvp.Value.ToString(), aryDefList, "|"))
                            bResult = false;
                        pb.Value = (double)pct;
                    }
                }
            }

            if (!bResult)
            {
                GreenLight.Visibility = Visibility.Collapsed;
                RedLight.Visibility = Visibility.Visible;
                tbResult.Text += l_strErrorMessage;
                l_bFileValidated = false;
            }
            if (l_strErrorMessage.Length > 0)
            {
                tbResult.Text += "\n";
                tbResult.Text += l_strErrorMessage;
            }

            tbResult.Text += "\n";
            tbResult.Text += Report();
            l_lngCurLineNumber = 0;
            l_lngLineCount = 0;
            pct = 0;
            dicBadData.Clear();
            dicDataStore.Clear(); 
            l_bFileValidated = false;        
        }
 

djcohen66
djcohen66

Member

Member

0 points

3 Posts

Re: How can I refresh the UI view of a user control from a silverlight class

I was wondering if I should use a background worker (as described in your other thread), to do the file open and validation.  Do you think that is an appropriate solution?

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP
Answered Question

Re: Re: How can I refresh the UI view of a user control from a silverlight class

Yes, you could use a background worker. I'd suggest you wrap the background worker and other code in a seperate class with a percent complete property. Then update that property and implement INotifyPropertyChanged so that you can then bind the progress bar value to the percent complete property. That way the binding will be responsible for updating the UI.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities