Windows phone 如何在Windows Phone中添加UIelement列表

Windows phone 如何在Windows Phone中添加UIelement列表,windows-phone,uielement,Windows Phone,Uielement,我想在windows phone中创建具有动态控件的页面。 在这样做的同时,我还想显示一个进度条 下面是我的代码 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); progressstackPanel.Visibility = Visibility.Visible;//progress bar fo

我想在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)时
它将得到一个异常。

我解决了这个问题,“xTitlePanel”是我在XAML中创建的
堆栈面板。我发现您不能从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);
filteredList=newlist();
if(frm_getset.ChildList!=null)
{
字符串[]arr=frm_getset.ChildList.Split(',');
foreach(arr中的字符串x)
{
添加(_template.list_fromgetset.Where(p=>p.FormID.Contains(x.Trim())).ToList()[0]);
}
}
xTbox_FormNameHeader.Text=frm_getset.NAME;
_属性=新列表();
_Attribute=frm_getset.list_attributegetset;
xTitlePanel.Children.Clear();
StackPanel spPanel=新的StackPanel();
spPanel.Orientation=System.Windows.Controls.Orientation.Vertical;
spPanel.Background=新的SolidColorBrush(Colors.Transparent);
//添加(PrepareControls.getControl(1,“LABEL”,“16”));
int i=1;
//列表_Attribute2=新列表();
foreach(AttributeGetSet a in_属性)
{
PrepareControls prepcontrol=新的PrepareControls();
StackPanel sp=prepcontrol.getControl(i,a.Label,a,this);
尝试
{
spPanel.Children.Add(sp);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
//xTitlePanel.Background=新的SolidColorBrush(Colors.White);
//_属性2.添加(a);
i+=1;
}
xTitlePanel.Children.Add(spPanel);

能否显示完整的代码段及其相关的XAML。