2015-10-28 30 views
0

下面是我的UWP應用媒體元素的XAML代碼,但它沒有提供自定義媒體控件,爲什麼?UWP自定義媒體控件不起作用

 <MediaElement x:Name="Media" Grid.Row="1" AutoPlay="True" AreTransportControlsEnabled="True" > 
      <MediaElement.TransportControls> 
       <MediaTransportControls Background="#FFF5F509" Foreground="#FF0625EA"/> 
      </MediaElement.TransportControls> 

    </MediaElement> 

回答

4

雖然MediaTransportControlsBackgroundForeground屬性,但設置這些屬性不會影響MediaTransportControls外觀。因爲默認MediaTransportControls使用ThemeResource中定義的ColorBrush

您可以在MediaTransportControls styles and templates找到MediaTransportControls的模板或generic.xaml(典型值在C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic)搜索 「MediaTransportControls」。

形成它的模板,我們可以發現其BackgroundForeground被設爲某些ThemeResource如:

<Grid x:Name='ControlPanelGrid' 
      Background='{ThemeResource SystemControlBackgroundChromeMediumBrush}' 
      VerticalAlignment='Bottom' 
      RenderTransformOrigin='0.5,0.5'> 

如果我們要使用MediaTransportControlsBackgroundForeground屬性來定製媒體傳輸控制,我們需要設置爲BackgroundForeground{TemplateBinding Foreground}。對於某些房產,如Background,可能很容易。你只需要找到Grid名爲「ControlPanelGrid」,改變其Background類似以下內容:

<Grid x:Name='ControlPanelGrid' 
      Background='{TemplateBinding Background}' 
      VerticalAlignment='Bottom' 
      RenderTransformOrigin='0.5,0.5'> 

但對於像Foreground的財產,這是複雜的。由於Foreground在許多子Style中定義,並且它們在不同樣式中具有不同的值。而在WinRT中,它不支持Setter.Value的Binding用法。所以你必須逐個設置{TemplateBinding Foreground}。這裏我用AppBarButton<CommandBar.PrimaryCommands>例如:

<AppBarButton x:Name="StopButton" 
       Foreground="{TemplateBinding Foreground}" 
       Icon="Stop" 
       MediaTransportControlsHelper.DropoutOrder="1" 
       Style="{StaticResource AppBarButtonStyle}" 
       Visibility="Collapsed" /> 
<AppBarButton x:Name="RewindButton" 
       Foreground="{TemplateBinding Foreground}" 
       MediaTransportControlsHelper.DropoutOrder="2" 
       Style="{StaticResource AppBarButtonStyle}" 
       Visibility="Collapsed"> 
    <AppBarButton.Icon> 
     <FontIcon Glyph="&#xEB9E;" /> 
    </AppBarButton.Icon> 
</AppBarButton> 
... 

在此之後,你可以把這個<Application.Resources>風格,給這個style一個x:key<Style x:Key="MyMediaTransportControlsStyle" TargetType="MediaTransportControls">。然後你就可以在MediaTransportControls使用這種新的風格:

<MediaElement x:Name="mediaElement" 
       Margin="5" 
       HorizontalAlignment="Stretch" 
       AreTransportControlsEnabled="True" 
       AutoPlay="False"> 
    <MediaElement.TransportControls> 
     <MediaTransportControls Background="Red" Foreground="White" Style="{StaticResource MyMediaTransportControlsStyle}" IsStopButtonVisible="True" IsStopEnabled="True" IsTextScaleFactorEnabled="True" IsPlaybackRateEnabled="True" IsPlaybackRateButtonVisible="True" IsFastForwardButtonVisible="True" IsFastForwardEnabled="True" IsFastRewindButtonVisible="True" IsFastRewindEnabled="True" /> 
    </MediaElement.TransportControls> 
</MediaElement> 

MediaTransportControls將使用在其BackgroundForeground屬性設置的顏色。

enter image description here

+0

真的很好的解釋和精彩的回答的人,真的很感激,謝謝:)生病送還給你,如果我有任何關於本:) –

+0

我想你的方法,但使用更多的問題** 10.0.10586.0 /通用**。有一個新的AppBarButtonStyle沒有Foreground屬性集。所有的AppBarButton都有這種風格。如果我爲自己設置了Foreground Property,它會給出錯誤,並且在運行App時,調試器會使其崩潰。任何想法如何解決這個問題? – yalematta

+1

@LayaleMatta:AFAIK,10.0.10240.0和10.0.10586.0中'MediaTransportControls'的樣式是相同的。我的方法也應該在10.0.10586.0。如果您的方法仍然存在問題,您可以使用您使用的XAML代碼提出新問題。謝謝。 –