【发布时间】:2019-09-02 19:29:40
【问题描述】:
我需要在 MediaPlayerElement 中实现自定义控件。我已经关注"Create custom transport controls" Microsoft guide 并尝试自己一步一步地制作所有内容,甚至公然从示例应用程序中复制代码。但这些都不起作用。
我只看到没有任何控件的 MediaPlayerElement。然后我试着少走一点。我创建了一个新项目并尝试自己复制示例应用程序。好吧,它也失败了。我重新意识到我在某个地方有错误,但我看不到那里。三重检查所有内容,甚至将默认样式复制到我的“自定义”样式中。但是,仍然没有面板。
控制类:
namespace Kinopub.UI.Utilities
{
public sealed class CustomMediaTransportControls : MediaTransportControls
{
public event EventHandler<EventArgs> Liked;
public CustomMediaTransportControls()
{
this.DefaultStyleKey = typeof(CustomMediaTransportControls);
}
protected override void OnApplyTemplate()
{
// This is where you would get your custom button and create an event handler for its click method.
Button likeButton = GetTemplateChild("LikeButton") as Button;
likeButton.Click += LikeButton_Click;
base.OnApplyTemplate();
}
private void LikeButton_Click(object sender, RoutedEventArgs e)
{
// Raise an event on the custom control when 'like' is clicked
Liked?.Invoke(this, EventArgs.Empty);
}
}
}
控制资源字典:(full code)
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kinopub.UI.Utilities">
<!-- Default style for MediaTransportControls -->
<Style TargetType="local:CustomMediaTransportControls">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FlowDirection" Value="LeftToRight" />
<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
<Setter Property="IsTextScaleFactorEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomMediaTransportControls">
<Grid x:Name="RootGrid" Background="Transparent">
...
A whole lot of code, copied from generic.xml
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
媒体播放器页面:
<Page
x:Class="Kinopub.UI.Views.MediaPlayerPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Kinopub.UI.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vms="using:Kinopub.UI.ViewModels"
xmlns:utils="using:Kinopub.UI.Utilities"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
<vms:MediaPlayerVM/>
</Page.DataContext>
<Grid>
<MediaPlayerElement
x:Name="PlayerElement"
Source="{Binding VideoMediaSource}"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
AreTransportControlsEnabled="True"
>
<MediaPlayerElement.TransportControls>
<utils:CustomMediaTransportControls>
</utils:CustomMediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
</Grid>
</Page>
【问题讨论】: