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

时间:2015-09-16 05:52:24

标签: c# xaml windows-phone-8

在Windows手机的用户控件中以编程方式生成按钮,并将此用户控件用于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>

1 个答案:

答案 0 :(得分:0)

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);
    }
}
  
    

P.S:我不知道您的确切需要,所以我采用静态方法添加按钮。