You have a few options here:
1. Font Icons
There is a much better way to accomplish what you are trying to do using Font Icons, e.g. Material Design Icons. Here is a cheat sheet for the icons.
This is not strictly a dependency, if you add the fonts as a resource (which you need to do in either case using PNGs, SVGs or - in this case - an image font).
<Button x:Name="OnPlayPauseButtonClicked" Grid.Row="1" Text="󰐊" VerticalOptions="End" Clicked="OnPlayPauseButton_Clicked"
FontFamily="materialdesignicons">
<Button.Triggers>
<DataTrigger TargetType="Button"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Playing}">
<!-- Pause Button Icon -->
<Setter Property="Text" Value="󰏤"/>
</DataTrigger>
<DataTrigger TargetType="Button"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Paused}">
<!-- Play Button Icon -->
<Setter Property="Text" Value="󰐊"/>
</DataTrigger>
<DataTrigger TargetType="Button"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Buffering}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Button.Triggers>
</Button>
James Montemagno also has a great blog post on this: https://montemagno.com/using-font-icons-in-xamarin-forms-goodbye-images-hello-fonts/
2. ImageButton
You can add your images to your project's resources and load them into an ImageButton
. How to use ImageButtons refer to this link: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/imagebutton.
SVGs
There is a library that allows you to load SVGs, this will require the following nuget to be installed: https://github.com/luberda-molinet/FFImageLoading/
<ImageButton x:Name="OnPlayPauseButtonClicked" Grid.Row="1" VerticalOptions="End" Clicked="OnPlayPauseButton_Clicked">
<ImageButton.Triggers>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Playing}">
<!-- Pause Button Icon -->
<Setter Property="Source">
<Setter.Value>
<ffimageloadingsvg:SvgCachedImage Source="resource://YourAppName.Resources.Pause.svg"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Paused}">
<!-- Play Button Icon -->
<Setter Property="Source">
<Setter.Value>
<ffimageloadingsvg:SvgCachedImage Source="resource://YourAppName.Resources.Play.svg"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Buffering}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</ImageButton.Triggers>
</ImageButton>
PNGs
To see how to add images to your app, refer to this page: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#embedded-images
<ImageButton x:Name="OnPlayPauseButtonClicked" Grid.Row="1" VerticalOptions="End" Clicked="OnPlayPauseButton_Clicked">
<ImageButton.Triggers>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Playing}">
<!-- Pause Button Icon -->
<Setter Property="Source" Value="{local:ImageResource Pause.PNG}" />
</DataTrigger>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Paused}">
<!-- Play Button Icon -->
<Setter Property="Source" Value="{local:ImageResource Play.PNG}" />
</DataTrigger>
<DataTrigger TargetType="ImageButton"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Buffering}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</ImageButton.Triggers>
</ImageButton>
[Update] 3: Use SvgCachedImage directly
You can also use SvgCachedImage
directly instead of a Button and attach a TapGestureRecognizer
to it:
<ffimageloadingsvg:SvgCachedImage x:Name="OnPlayPauseButtonClicked" Grid.Row="1" VerticalOptions="End">
<ffimageloadingsvg:SvgCachedImage.Triggers>
<DataTrigger TargetType="ffimageloadingsvg:SvgCachedImage"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Playing}">
<!-- Pause Button Icon -->
<Setter Property="Source"
Value="resource://YourAppName.Resources.Pause.svg"/>
</DataTrigger>
<DataTrigger TargetType="ffimageloadingsvg:SvgCachedImage"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Paused}">
<!-- Play Button Icon -->
<Setter Property="Source"
Value="resource://YourAppName.Resources.Play.svg"/>
</DataTrigger>
<DataTrigger TargetType="ffimageloadingsvg:SvgCachedImage"
Binding="{Binding CurrentState}"
Value="{x:Static xct:MediaElementState.Buffering}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</ffimageloadingsvg:SvgCachedImage.Triggers>
<ffimageloadingsvg:SvgCachedImage.GestureRecognizers>
<TapGestureRecognizer Tapped="OnPlayPauseButton_Clicked"/>
</ffimageloadingsvg:SvgCachedImage.GestureRecognizers>
</ffimageloadingsvg:SvgCachedImage>