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

标签 c# xaml windows-phone-8

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

用户控件.xaml:

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

用户控件.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;   
                } 
            }
        }

主页.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>

最佳答案

用户控件.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.

用户控件.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.

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

相关文章:

c# - 对象名称 dbo 表名无效 - Entity Framework 适用于 IIS,但不适用于 Azure 应用服务

wpf - 如何在 XAML 中绘制一个 'felt pen' 样式的圆而不结束连接

silverlight - 如何在 ListBox 中的 DataTemplate 中获取 TextBox 以通知 ViewModel 值更改

windows-8 - Jquery 不适用于我在 Visual Studio 2012 中使用 HTML 5 模板创建的 Windows 8 Phone 应用程序

windows-phone-8 - 获取 Windows Phone 8 设备友好名称的 API?

c# - 术语 'Connect-AzureAD' 未被识别为 cmdlet 的名称

c# - 全局 ASP.NET Web API 过滤器是否总是按顺序执行?

c# - 使用 C# 在富文本框中打开文件

c# - Xamarin 形成细胞操作 RTL

c# - Frame.navigate 和 NagivationService 不工作