0

I want to create a page with dynamic control in windows phone. While doing this I also want to show a progress bar

Below is my code

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    progressstackPanel.Visibility = Visibility.Visible;//progress bar
    formScreen = this;

    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        if (!isfst)
        {
             DrawScreen();
        }
        else
        {
            //xTitlePanel is only stack panel in my xaml with vertical orientation
            xTitlePanel.UpdateLayout();
        }
        isfst = true;
        progressstackPanel.Visibility = Visibility.Collapsed;
    });
}

//Code of DrawScreen which is adding control to my stack panels    
private void DrawScreen()
{
     if (frm_getset.ChildList != null)
     {
          String[] arr = frm_getset.ChildList.Split(',');

          xTitlePanel.Children.Clear();

          PrepareControls prepcontrol = new PrepareControls();

          foreach (AttributeGetSet a in _Attribute)
          {
              //this will return a stackpanel containing 
              // button/textbox etc.depending on a
              StackPanel sp = prepcontrol.getControl(i, a.Label, a, formScreen);
              try
              {
                   xTitlePanel.Children.Add(sp);

                   ///Here I get a eception only one control is added first one
                   /// for anyone it is getting a exception Argument    
               }
               catch(Exception ex)
               {
                    Console.WriteLine(ex.Message);
               }

               i += 1;
           }

The system is adding only one control and when ever it try to execute xTitlePanel.Children.Add(sp); it will get an exception.

rene
  • 41,474
  • 78
  • 114
  • 152
Koushik
  • 345
  • 1
  • 7

1 Answers1

0

I solved the problem ,"xTitlePanel" is a StackPanel I created in my XAML. I found you can not add more that one element from Dispatcher to a control crated on xaml. Like that. so I have to create local stack and add controls to the that local stack panel then and after complete I add the local stack panel to xTitlePanel. NOW my code looks like below

filteredList = new List<FormGetSet>();
            if (frm_getset.ChildList != null)
            {
                String[] arr = frm_getset.ChildList.Split(',');

                foreach (String x in arr)
                {
                    filteredList.Add(_template.list_fromgetset.Where(p => p.FormID.Contains(x.Trim())).ToList()[0]);
                }
            }
            xTbox_FormNameHeader.Text = frm_getset.NAME;
            _Attribute = new List<AttributeGetSet>();
            _Attribute = frm_getset.list_attributegetset;

            xTitlePanel.Children.Clear();

            StackPanel spPanel = new StackPanel();
            spPanel.Orientation = System.Windows.Controls.Orientation.Vertical;
            spPanel.Background = new SolidColorBrush(Colors.Transparent);
            //xTitlePanel.Children.Add(PrepareControls.getControl(1, "LABEL", "16"));
            int i = 1;
           // List<AttributeGetSet> _Attribute2 = new List<AttributeGetSet>();
            foreach (AttributeGetSet a in _Attribute)
            {
                PrepareControls prepcontrol = new PrepareControls();
                StackPanel sp=  prepcontrol.getControl(i, a.Label, a, this);
                try
                {
                    spPanel.Children.Add(sp);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                //xTitlePanel.Background = new SolidColorBrush(Colors.White);
                //_Attribute2.Add(a);
                i += 1;
            }
         xTitlePanel.Children.Add(spPanel);
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Koushik
  • 345
  • 1
  • 7