正在做的项目里用到了WPF做界面,其中有个功能是呈现播放器的开始暂停按钮功能,即实现开始和暂停图片的来回切换,网上查了很久,终于做出来了,特来分享。
前台:
<Border Height="80px" Width="80px" Margin="35,0,0,0" HorizontalAlignment="Left" CornerRadius="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="Orange" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Image x:Name="stopplayPic" Source="Assets/img/stop.png" Margin="6,21,7,18" UIElement.MouseLeftButtonDown="image_MouseDown_stop" ></Image>
</Grid>
</Border>
后台:
using System.Windows.Media.Imaging;//这个引用我找了很久...一直都不对
bool isPlay = true;
private void image_MouseDown_stop(object sender, MouseEventArgs e)
{
if (isPlay)
{
BitmapImage img = new BitmapImage(new Uri("Assets/img/stop.png", UriKind.Relative));//这里是相对路径
stopplayPic.Source = img;
}
else
{
BitmapImage img = new BitmapImage(new Uri("Assets/img/play.png", UriKind.Relative));
stopplayPic.Source = img;
}
isPlay =!isPlay;
}
效果:
——点击——>
——点击——>
——点击——>
......
成功~~
版权声明:本文为博主原创文章,未经博主允许不得转载。