2013-03-15 21 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); 
相關問題