我需要在 MediaPlayerElement 中实现自定义控件。我遵循了“创建自定义传输控件”Microsoft 指南,并尝试自己一步一步地制作所有内容,甚至公然从示例应用程序中复制代码。但这些都没有奏效。
我只看到没有任何控件的 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);
}
}
}
控制资源字典:(完整代码)
<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>
通过检查您的代码,您需要注意两件事。
Kinopub.UI.Utilities
到App.xaml
,例如:<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Kinopub/UI/Utilities/resource_file_name.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
AppBarButton
名为LikeButton 的按钮ControlTemplate
:<CommandBar x:Name="MediaControlsCommandBar" ...>
...
<AppBarButton x:Name="LikeButton"
Icon="Like"
Style="{StaticResource AppBarButtonStyle}"
MediaTransportControlsHelper.DropoutOrder="3"
VerticalAlignment="Center"
/>
...
</CommandBar>
最好的祝福。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句