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#方法不会生成没有参数的重载?

c# - 通用运算符的有效方法

c# - 如何使用 Lazy<StackFrame> 创建可预测的输出

c# - WPF-文本 block /标签的数据网格行验证错误?

c# - 找不到类型或命名空间名称 'Google'

c# - 使用 XAML 属性在 RichTextBox 中插入图像

c# - 使用 ID 或操作名称的 ASP.NET MVC 路由

xaml - Winrt 中的网格效果

c# - 如何添加选项以有选择地从ComboBox删除项目?

android - 强制HTML5应用处于横向模式