首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有一些动态按钮的Wp8应用程序自定义控件,在主页上没有显示任何内容。

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

Stack Overflow用户
提问于 2015-09-16 05:52:23
回答 1查看 69关注 0票数 2

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

usercontrol.xaml:

代码语言:javascript
复制
 <ScrollViewer >
 <StackPanel x:Name="Panel">
 <ContentControl x:Name="container"></ContentControl>
 </StackPanel>
 </ScrollViewer>

usercontrol.xaml.cs:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-16 07:17:34

usercontrol.xaml

代码语言:javascript
复制
<ScrollViewer>
   <ContentControl x:Name="container">
       <StackPanel x:Name="Panel">
       </StackPanel>
   </ContentControl>
</ScrollViewer>

没有必要再次在usercontrol的构造函数中创建堆栈面板和内容控制,因为它们已经存在于usercontrol结构中。Contentcontrol可以保存最后分配给它的内容,所以我把stackpanel放到contentcontol中。

usercontrol.xaml.cs

代码语言:javascript
复制
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);
    }
}

记者:我不知道你的确切需求,所以我采取了静态的方法添加按钮。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32600719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档