我正在尝试为Mediaplayer应用程序创建自定义媒体传输控件,我可以在其中添加自己的按钮和功能。我唯一需要的标准 MediaTransportControl 是 SeekBar 。
我尝试摆弄字典,因为它太复杂了,以至于我无法获得所要获得的好处。有没有简单的方法可以做到这一点,还是我应该放弃?
谢谢!
答案 0 :(得分:1)
好,知道了!
这是食谱:
添加一个单独的XAML文件来保存新传输栏的资源字典。将CustomMediaTransport示例中的文件复制到其中。确保将x:local =“ using:...”更改为您自己的项目名称空间。
在XAML代码的CommandBar部分中添加所需的按钮(您将在此处看到其他命令栏按钮。自定义命令栏按钮的格式为:
<AppBarButton x:Name='Plus500Button'
Label="Very Long Skip Forward"
VerticalAlignment="Center"
Style='{StaticResource AppBarButtonStyle}'>
<AppBarButton.Icon>
<BitmapIcon UriSource="put in here the asset for your button icon"/>
</AppBarButton.Icon>
</AppBarButton>
在单独的CS文件中添加新按钮的代码。用解决方案的名称空间替换yournamespace。
namespace yournamespace
{
public sealed class CustomMediaTransportControls : MediaTransportControls
{
//Add an eventhandler for each button you added
public event EventHandler<EventArgs> Plus500;
public CustomMediaTransportControls()
{
this.DefaultStyleKey = typeof(CustomMediaTransportControls);
}
//Add an override for each button you created in the following format:
protected override void OnApplyTemplate()
{
Button Plus500Button = GetTemplateChild("Plus500Button") as Button;
Plus500Button.Click += Plus500Button_Click;
}
//To raise an event when the button is clicked, add the following code for each button you added:
private void Plus500B_Click(object sender, RoutedEventArgs e)
{
Plus500?.Invoke(this, EventArgs.Empty);
}
最后,以以下方式在XAML代码中使用新的自定义传输栏:
<MediaPlayerElement x:Name="mediaPlayerElement"
Grid.ColumnSpan="2"
Grid.RowSpan="4"
AutoPlay="True"
AreTransportControlsEnabled="True"
<MediaPlayerElement.TransportControls >
<local:CustomMediaTransportControls IsCompact="False"
IsPlaybackRateButtonVisible="True"
IsEnabled="True"
Plus500="Plus500" <!-- Use the name you added in the EventHandler you added for the cuscom command bar
</local:CustomMediaTransportControls>
</MediaPlayerElement.TransportControls>
瞧瞧!鲍勃是你母亲的兄弟!
答案 1 :(得分:0)
有两种方法可以向媒体播放器的控件添加自定义功能。
Custom Transport Controls:您可以在其中编辑模板的样式,或向其中添加额外的按钮并为其添加功能,也可以删除现有按钮或折叠它们(如果不希望的话)。您甚至可以直接从传输控件设置某些按钮的可见性,而无需使用诸如 IsZoomButtonVisible 等属性的样式。
不要使用sdk的内置传输控件,并将MediaPlayerElement控件的 AreTransportControlsEnabled 设置为false,然后创建自己的用户控件来控制媒体,这实际上更加困难你是一个初学者。
我将建议您选择第一个选项,只需逐步阅读文档即可,一旦您了解了如何将它们组合在一起并一起工作的基础知识,便可以轻松地向控件添加很多自定义按钮。您不需要弄乱样式,只需将按钮添加到样式的命令栏,然后向其添加单击处理程序,所有这些都记录在我上面提供的链接中,请逐步尝试,并在有问题的地方进行尝试,请继续在此处询问质量问题。
答案 2 :(得分:0)
好吧.....我摆弄了几个小时。我已经阅读了来自touseefbsb的以上链接,并逐行阅读了CustomMediaTransportControl示例。
我创建了一个词典,并从示例单词中复制了该词典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MultiVid">
<!--This is the style for a custom Media Transport Control. We are taking the default control and simply tweaking it to fit our needs.
The default template can be found here: C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10069.0\Generic
Simply take the portion of code that corresponds to the MediaTransportControl and bring it in to your app just like we have done in this sample-->
<Style TargetType="local:CustomMediaTransportControls">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FlowDirection" Value="LeftToRight" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="IsTextScaleFactorEnabled" Value="False" />
<Setter Property="Template">
----- etc..... it's a long file but it's the exact copy of the "generic.xaml" file of the Sample except for the beginning where I used xmlns:local="using:MultiVid" as my solution is called MultiVid.
</Style>
</ResourceDictionary>
然后,按照教程创建类:
namespace MultiVid
{
public sealed class CustomMediaTransportControls : MediaTransportControls
{
public event EventHandler<EventArgs> Moins10click;
public CustomMediaTransportControls()
{
this.DefaultStyleKey = typeof(CustomMediaTransportControls);
}
protected override void OnApplyTemplate()
{
// Find the custom button and create an event handler for its Click event.
var Moins10Button = GetTemplateChild("Moins10") as Button;
Moins10Button.Click += Moins10Button_Click;
base.OnApplyTemplate();
}
private void Moins10Button_Click(object sender, RoutedEventArgs e)
{
var handler = Moins10click;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}
最后是我的MainPage Xaml:
<Page
x:Class="MultiVid.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:MultiVid"
mc:Ignorable="d"
Loaded="initkbd"
Unloaded="unloadkbd">
<Grid x:Name="imggrid" Background="Black" BorderBrush="Black" >
<MediaPlayerElement x:Name="mediaPlayerElement" AutoPlay="True" Height="Auto" Width="Auto" AreTransportControlsEnabled="True" RequestedTheme="Dark" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<MediaPlayerElement.TransportControls>
<local:CustomMediaTransportControls IsCompact="False"
IsZoomButtonVisible="True"
IsZoomEnabled="True"
IsPlaybackRateButtonVisible="True"
IsEnabled="True">
</local:CustomMediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
</Grid>
</Page>
当我将以上代码与C#代码一起使用时,根本看不到TransportControl。如果我将该部分注释掉,则会看到传输控制。
5个小时后,我还是听不到...五月天有人!