Search code examples
silverlightmedia-playermedia

How to dynamically add buttons and Silverlight player in a Grid?


I'm basically trying to create the following set of code dynamically/programmatically but I'm unsure of how to do it.

    <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>

Here's how it looks statically:


Solution

  • First of all, you must give a name to your StackPanel like this;

    <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>
    

    And then, you must add the following lines in code-behind;

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

    I hope this will be help to you!