2013-03-15 28 views
0

我想在windows phone中创建一个包含动态控制的页面。 虽然这样做我也想显示一个进度条如何在Windows Phone中添加UIelement列表

下面是我的代码

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);它会得到一个异常。

+0

请问您能否显示完整的代码段和它所涉及的XAML。 – 2013-03-15 21:12:35

回答

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); 
相关问题