gpt4 book ai didi

c# - 访问元素组件 silverlight

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:15 24 4
gpt4 key购买 nike

我正在 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)
{

}

如何在代码隐藏中访问播放按钮?

最佳答案

附加到 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));
}
}

关于c# - 访问元素组件 silverlight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20190229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com