Search code examples
c#uwpcontrolsmedia-playeruwp-xaml

How can I easily hide all transport controls in UWP MediaPlayerElement except a few?


I have a MediaPlayerElement that plays a video automatically and I want the user to be only able to seek in the video and press pause/stop/play.

It looks like I have to set AreTransportControlsEnabled to true and then hide all the controls I don't want one by one as per default all controls are visible.

So I did this:

<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True">
            <MediaPlayerElement.TransportControls>
                <MediaTransportControls
                    ShowAndHideAutomatically="True"
                    IsFullWindowButtonVisible="False"
                    IsNextTrackButtonVisible="False"
                    IsPreviousTrackButtonVisible="False"
                    IsVolumeButtonVisible="False"
                    IsZoomButtonVisible="False"
                    IsFastForwardButtonVisible="False"
                    IsFastRewindButtonVisible="False"
                    IsPlaybackRateButtonVisible="False"
                    IsRepeatButtonVisible="False"
                    IsSkipBackwardButtonVisible="False"
                    IsSkipForwardButtonVisible="False"
                    Windows10version1803:IsCompactOverlayButtonVisible="False"
                    IsSeekBarVisible="True"
                    IsSeekEnabled="True"
                    IsStopButtonVisible="True"
                    />
            </MediaPlayerElement.TransportControls>
        </MediaPlayerElement>

For my taste this looks really cumbersome. Isn't there are setting like "hideall=true" and then I could only enable those I want. And for example, there seems to be no way to also hide the "cast to device" button, so with the current approach the user would always see this button, what I don't really like: Example with 'cast to device' button visible

Any ideas?


Solution

  • Removing "CastButton" from generic.xaml didn't work out for me. I found a solution for removing Cast to Device button in another forum : https://social.msdn.microsoft.com/Forums/windows/en-US/e3307864-f194-4197-9f0d-bb2b8cd7228c/uwp-custom-media-transport-controls-hide-custom-buttons

    Here is the working code for removing "CastButton" AppBarButton from Mediaplayer at runtime.

    public class CustomMediaTransportControls: MediaTransportControls
    {
        protected override void OnApplyTemplate()
        { 
            AppBarButton CastButton = GetTemplateChild("CastButton") as AppBarButton;
    
            var MediaControlsCommandBar = GetTemplateChild("MediaControlsCommandBar") as CommandBar;
            MediaControlsCommandBar.PrimaryCommands.Remove(CastButton); 
            base.OnApplyTemplate();
        }
    }