c# - WP8应用程序自定义控件具有一些动态按钮,主页上未显示任何内容

标签 c# xaml windows-phone-8

在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>



  
    无需在usercontrol的构造函数中再次创建stackpanel和contentcontrol,因为它们已经存在于usercontrol结构中。
    ContentControl可以容纳最后分配给它的内容,因此我将stackpanel放入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);
    }
}



  
    附言:我不知道您的确切需求,因此我采用了静态方法来添加按钮。

关于c# - WP8应用程序自定义控件具有一些动态按钮,主页上未显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32600719/

相关文章:

c# - 在 WPF View 框中对齐内容

c# - 如何将子对象从JArray放入ObservableCollection

WPF 绑定(bind) : Casting in binding path

c# - 带有 Asp.net Mvc StringLength 属性的正则表达式

c# - 从JavaScript调用ASP生成的按钮单击

c# - 从数据集中检索列的值时出错

c# - datatemplate 绑定(bind)图片刷源

visual-studio - Visual Studio - 找不到 "export template"选项

azure - WP Silverlight BackgroundTask 与 Azure 通信

c# - 如何使用代码协定来确保始终从方法返回非空白、非空值?