如何在网格中动态添加按钮和Silverlight播放器?

问题描述 投票:0回答:2

我基本上试图动态/编程地创建以下代码集,但我不确定如何做到这一点。

    <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <smf:SMFPlayer x:Name="player" Grid.Row="0" AutoPlay="False">
        <smf:SMFPlayer.Playlist>
            <media:PlaylistItem 
                DeliveryMethod="AdaptiveStreaming" 
                MediaSource="http://video3.smoothhd.com.edgesuite.net/ondemand/Big%20Buck%20Bunny%20Adaptive.ism/Manifest"/>
            <media:PlaylistItem 
                DeliveryMethod="AdaptiveStreaming" 
                SelectedCaptionStreamName="textstream_eng"
                MediaSource="http://streams.smooth.vertigo.com/elephantsdream/Elephants_Dream_1024-h264-st-aac.ism/manifest"/>
        </smf:SMFPlayer.Playlist>
    </smf:SMFPlayer>

    <StackPanel Grid.Row="1" Orientation="Horizontal" Background="Transparent">
        <Button x:Name="test1" Height="30" Width="70" Content="Test 1"/>
        <Button x:Name="test2" Height="30" Width="70" Content="Test 2"/>
    </StackPanel>
</Grid>

这是静态的看法:

silverlight media-player media
2个回答
1
投票

首先,你必须像这样给你的StackPanel命名;

<StackPanel x:Name="spBottom" Grid.Row="1" Orientation="Horizontal" Background="Transparent">
        <Button x:Name="test1" Height="30" Width="70" Content="Test 1"/>
        <Button x:Name="test2" Height="30" Width="70" Content="Test 2"/>
</StackPanel>

然后,您必须在代码隐藏中添加以下行;

For iLoop As Integer = 0 to 4
     Dim btn As New Button With {.Content = "Button" & iLoop}
     spBottom.Children.Add(btn)
Next iLoop

我希望这会对你有所帮助!


0
投票

没有xmlns(XML名称空间)前缀的控件可以在代码隐藏中创建,而无需添加任何用法。例如,在C#中,您可以使用以下代码从XAML重新创建StackPanel:

StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal, Background = null };
panel.SetValue(Grid.RowProperty, 2);
LayoutRoot.Children.Add(panel);

带有xmlns前缀的元素,带有冒号的任何元素,例如<smf:,都需要知道代码隐藏中的命名空间。关联的命名空间在第一个元素中定义,看起来像xmlns:smf="PathToTheNamespace"。通过在顶部添加using PathToTheNamespace语句,通常会在C#中的代码隐藏文件中引用此命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.