如何在Windows Phone中添加UIelement列表

时间:2013-03-15 06:01:59

标签: windows-phone uielement

我想在Windows Phone中创建一个带动态控件的页面。 在这样做时我还想显示一个进度条

以下是我的代码

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;
           }

系统只添加一个控件,当它尝试执行xTitlePanel.Children.Add(sp);时,它将获得异常。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,“xTitlePanel”是我在XAML中创建的StackPanel。我发现你不能将Dispatcher中的一个元素添加到xaml上的控件中。像那样。所以我必须创建本地堆栈并将控件添加到该本地堆栈面板然后在完成后我将本地堆栈面板添加到xTitlePanel。现在,我的代码如下所示

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);