访问元素组件silverlight

时间:2013-11-25 10:48:27

标签: c# silverlight xaml code-behind

我正在开发基于MVVM模式的Silverlight 5的播放器。我正在尝试更改代码中的状态按钮。

文件: PlayerPage.xaml
我的播放器组件xaml:

 <Grid x:Name="LayoutRoot">
    <player:Player x:Name="Player"
                   Playlist="{Binding Playlist}"
                   CurrentPlaylistItem="{Binding CurrentPlaylistItem, Mode=TwoWay}"
                   IsSeekingEnabled="{Binding CanSeek}"
                   AutoLoad="True"
                   AutoPlay="True"
                   MediaEnded="Player_MediaEnded"
                   MediaOpened="Player_MediaOpened"
                   Style="{StaticResource PlayerStyle}"
                   PlaylistVisibility="Disabled"
                   LogLevel="Warning"
                   AutoHideControls="True"
                   AutoHideDelay="0:0:5"
                   AllowDoubleClickToggle="True"
                   RetryDuration="00:01:00"
                   RetryInterval="00:00:10"
                   BufferingTime="00:00:15"
        />

</Grid>

文件: PlayerPage.xaml
组件播放器的风格:

<Grid x:Name="ControllerContainer" Height="40" Grid.Row="4"    RenderTransformOrigin="0.5,0.5">
   <Grid.RenderTransform>
       <CompositeTransform />
            </Grid.RenderTransform>
               <Grid.Background>
                   <ImageBrush ImageSource="component/Images/grid_background_player.jpg"
                                    AlignmentY="Top" />
                         </Grid.Background>
                          <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="120" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid x:Name="playercontrols" Grid.Column="0" HorizontalAlignment="Left" Width="90">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0">
                                    <Button x:Name="TbPlayElement" Click="TbPlayElement_Click"  HorizontalAlignment="Left" Cursor="Hand" >
                                                <Grid>
                                                    <Image Source="component/Resources/play.png" Stretch="Uniform" Height="16" Width="16" />
                                                </Grid>
                                    </Button>

现在在 PlayerPage.cs 后面的代码中,当我按下停止按钮时,我想更改播放按钮的图像。

 private void TbStopElement_Click(object sender, System.Windows.RoutedEventArgs e)
    {

    }

如何在后面的代码中访问播放按钮?

3 个答案:

答案 0 :(得分:2)

附加到Loaded事件以保存图像的引用:

XAML:

<Image Source="component/Resources/play.png" Stretch="Uniform" Height="16" Width="16"
       Loaded="ImageLoaded" />

代码隐藏:

private Image imageReference;
private void ImageLoaded(object sender, EventArgs e)
{
    imageReference = sender as Image;
}

然后当您想要更改图像时:

private void TbStopElement_Click(object sender, System.Windows.RoutedEventArgs e)
{
    if(imageReference != null)
    {
        imageReference.Source = new BitmapImage(new Uri("/YourAppName;component/Resources/YourImage.png", UriKind.Relative));
    }
}

答案 1 :(得分:1)

我建议你这个代码

TbPlayElement.Source = new BitmapImage(new Uri("/TbPlayElement.content;component/NameNewImage.png", UriKind.Relative));

备注:在Visual Studio中检查xaml文件的属性,确保Build Action is set to Page

或者您可以使用ToggleButton

link:http://msdn.microsoft.com/en-us/library/cc296245%28v=vs.95%29.aspx

答案 2 :(得分:1)

访问TbStopElement,如下所示:

        var grid = TbPlayElement.Content as Grid;
        var playImage = grid.Children[0] as Image;

        BitmapImage bitmap = new BitmapImage();
        bitmap.UriSource = new Uri("newImage.png", UriKind.Relative);
        playImage.Source = bitmap;