lundi 7 décembre 2015
Re-purposing Media Player Framework (220.127.116.11) buttons to do something else (XAML,C#)
In this small example I will explain why I re-purposed some media player buttons on my MediaPlayer. At first I was looking into creating a whole new button and linking it with events using MVVM with a RelayCommand, I wanted to do this well of course.
To start I wanted to create my own button from scratch and bind it with an event, this quickly became difficult as I am not a great designer (looking at my apps you will quickly understand what I mean) and binding an event on events that are that deep (in a ControlTemplate) is not always the best idea or the easiest. It does work and it is doable, however I was sometimes getting some issues on the tapped event which was not being fired (probably the tapped event was getting caught by some other button in the player).
So I started looking at my XAML code to see what I could do:
The C# code for my player looked as follow:
and then it hit me! why dont I just re-use some of the buttons on the player that I am not using. This would allow my to not have to re code all of the different events linked to the button and also have the effects already coded for me like the on press event and mouse over events and more.
In my example I decided to re purpose the full screen button to start, all we need to do on the MediaPlayer is to show the FullScreenButton:
Then we just need to catch the isFullScreenChanged event like this:
And now you can do whatever you wish when the full screen button is clicked on the player.
Next we will edit the AppBarButton style so that we show our custom button, like I have written in previous articles you need to get the default style of the AppBarButton and edit it as you need. For this example we will just edit the element <Viewbox> comment its Ellipse and ContentPresenter and add an image:
Lastly we just need to link this style to our FullScreenButton which should be located in your ControlTemplate of the video media element as follow:
And there you have it, you have re purposed an AppBarButton to do what you need it to do. I used the FullScreenButton but you could have used any other button that you are not using as long as it has a Changed event linked to it!
As a great philosopher once said "Rien ne se perd, rien ne se crée, tout se transforme." Antoine Lavoisier