雖然MediaTransportControls
有Background
和Foreground
屬性,但設置這些屬性不會影響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」。
形成它的模板,我們可以發現其Background
和Foreground
被設爲某些ThemeResource
如:
<Grid x:Name='ControlPanelGrid'
Background='{ThemeResource SystemControlBackgroundChromeMediumBrush}'
VerticalAlignment='Bottom'
RenderTransformOrigin='0.5,0.5'>
如果我們要使用MediaTransportControls
的Background
和Foreground
屬性來定製媒體傳輸控制,我們需要設置爲Background
或Foreground
至{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="" />
</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
將使用在其Background
和Foreground
屬性設置的顏色。
真的很好的解釋和精彩的回答的人,真的很感激,謝謝:)生病送還給你,如果我有任何關於本:) –
我想你的方法,但使用更多的問題** 10.0.10586.0 /通用**。有一個新的AppBarButtonStyle沒有Foreground屬性集。所有的AppBarButton都有這種風格。如果我爲自己設置了Foreground Property,它會給出錯誤,並且在運行App時,調試器會使其崩潰。任何想法如何解決這個問題? – yalematta
@LayaleMatta:AFAIK,10.0.10240.0和10.0.10586.0中'MediaTransportControls'的樣式是相同的。我的方法也應該在10.0.10586.0。如果您的方法仍然存在問題,您可以使用您使用的XAML代碼提出新問題。謝謝。 –