Wp8 应用程序自定义控件,其中有一些动态按钮,在主页上不显示任何内容

Wp8 application custom control which has some dynamic buttons , shows nothing on mainpage

在 windows phone 的用户控件中以编程方式生成按钮并将此用户控件用于 mainpage.xaml 时出现问题,但在应用程序运行时没有显示任何按钮。 这是我正在使用的代码片段,谢谢!

usercontrol.xaml:

 <ScrollViewer >
 <StackPanel x:Name="Panel">
 <ContentControl x:Name="container"></ContentControl>
 </StackPanel>
 </ScrollViewer>

usercontrol.xaml.cs:

public LoginInterfaceControl()
    {
        InitializeComponent();
        this.container = new ContentControl();
        this.Panel = new StackPanel();
    }
    public LoginInterfaceControl(string Api_key)
    {
        InitializeComponent();
        this.Panel = new StackPanel();
        this.container = new ContentControl();
        loginWP_DownloadString(Api_key);

    }
    public async void loginWP_DownloadString(string key)
    {
        InitializeComponent();
        string cont;
        using (HttpClient client = new HttpClient())
        {   
         var result = await client.GetAsync("http://cdn.loginradius.com/interface/json/" + key + ".json");
            if (result.StatusCode == HttpStatusCode.OK)
            {
                cont = await result.Content.ReadAsStringAsync();
                MessageBox.Show(cont);
            }
            else
            {
                cont = await result.Content.ReadAsStringAsync();
                MessageBox.Show(cont);
            }
            if (!string.IsNullOrEmpty(cont))
            { 
            var root1 = JsonConvert.DeserializeObject<RootObject>(cont);
                int no = 1;
                foreach (var provider in root1.Providers)
                {
                    no++;
                    Button newBtn = new Button()
                    {
                        Content = provider.Name.ToString(),
                        Name = provider.Name.ToString(),
                        //Width = 88,
                        //Height = 77,
                        Visibility = System.Windows.Visibility.Visible,
                        //Margin = new Thickness(5 + 20, 5, 5, 5),
                        Background = new SolidColorBrush(Colors.Black),
                        VerticalAlignment =VerticalAlignment.Center,
                        Opacity=0.5

                    };
                    newBtn.Click += google_click;
                   System.Windows.Visibility.Visible;
                    container.Opacity = 0.5;
                    this.container.Content = newBtn;   
                } 
            }
        }

Mainpage.xaml:

<Grid xmlns:src="clr-namespace:LRDemo" 
    Background="White" Margin="10,0,-10,186" Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <src:LoginInterfaceControl Grid.Row="0"/>
            <!--<src:LoginInterfaceControl Grid.Row="1" Margin="0,15,0,0"/>-->
        </Grid>

usercontrol.xaml

<ScrollViewer>
   <ContentControl x:Name="container">
       <StackPanel x:Name="Panel">
       </StackPanel>
   </ContentControl>
</ScrollViewer>

there is no need to again create stackpanel and contentcontrol in constructor of usercontrol because they are already there in usercontrol structure. Contentcontrol can hold content that is assign to it last so I took stackpanel into contentcontol.

usercontrol.xaml.cs

public LoginInterfaceControl()
{
    this.InitializeComponent();
    abc();
}

public void abc()
{

    for (int i = 0; i <= 5; i++)
    {

        Button newBtn = new Button()
        {
           Content = "name" + i,
           Name = "name" + i,

           Background = new SolidColorBrush(Colors.Black),
           VerticalAlignment = VerticalAlignment.Center,
           Opacity = 0.5

        };

       newBtn.Click += newBtn_Click;

       container.Opacity = 0.5;
       this.Panel.Children.Add(newBtn);
    }
}

P.S : I do not know your exact need so I took static methods to add buttons.