2011-07-05 44 views
0

我基本上試圖動態/編程創建下面的代碼集,但我不確定如何去做。在網格中動態添加按鈕和Silverlight播放器?

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

這裏是它的外觀靜態: http://i.imgur.com/uz1O8.png

謝謝!

回答

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"。這個命名空間通常在C#的代碼隱藏文件中通過在頂部添加一個using PathToTheNamespace語句來引用。

+0

如果我有一個繼承自SMFPlayer的類,是否有任何方法指定我想通過xmlns使用此子類而不是SMFPlayer:...? – Neykho

+0

@Neykho,是的。您需要知道繼承自SMFPlayer的類的名稱空間路徑。然後添加新的xmlns或更改xlmns:smf以使用新路徑。即'xmlns:name =「clr-namespace:TypicalThisIsNameOfYourProject.RestOfTheNamespacePath」' – ShawnFeatherly