C# · 12月 26, 2021

c# – Windows Phone 8.1旋转自定义标题样式

我的目标是模仿类似的效果: http://www.visuallylocated.com/post/2012/05/23/Changing-the-background-color-of-your-pivot-headers.aspx.
有资源在线描述如何做,但所有这些都适用于Windows Phone 8. 8.1更新带来严重的API更改,使代码无用.

那么,怎样才能调整头文件头?我发现命名空间Windows.UI.Xaml.Controls.Primitives,其中包括类PivotHeaderPanel,这可能在这种情况下有帮助,但我找不到从XAML访问此类的方法.
还是还有另一种方式?

解决方法 如果您只想更改所有标题的背景颜色,那么您可以在Window Phone 8.1中进行操作.

首先,使用Expression Blend生成Pivot控件的默认样式.

<Thickness x:Key=”PivotPortraitThemePadding”>19,38,0</Thickness><Thickness x:Key=”PivotLandscapeThemePadding”>19,25,0</Thickness><Style x:Key=”CustomPivotStyle” TargetType=”Pivot”> <Setter Property=”Margin” Value=”0″/> <Setter Property=”Padding” Value=”0″/> <Setter Property=”Foreground” Value=”{ThemeResource PhoneForegroundBrush}”/> <Setter Property=”Background” Value=”Transparent”/> <Setter Property=”ItemsPanel”> <Setter.Value> <ItemsPanelTemplate> <Grid/> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”Pivot”> <Grid x:Name=”RootElement” Background=”{TemplateBinding Background}” HorizontalAlignment=”{TemplateBinding HorizontalAlignment}” VerticalAlignment=”{TemplateBinding VerticalAlignment}”> <Grid.RowDeFinitions> <RowDeFinition Height=”Auto”/> <RowDeFinition Height=”*”/> </Grid.RowDeFinitions> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name=”Orientation”> <VisualState x:Name=”Portrait”> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=”Margin” Storyboard.TargetName=”TitleContentControl”> <DiscreteObjectKeyFrame KeyTime=”0″ Value=”{ThemeResource PivotPortraitThemePadding}”/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name=”Landscape”> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty=”Margin” Storyboard.TargetName=”TitleContentControl”> <DiscreteObjectKeyFrame KeyTime=”0″ Value=”{ThemeResource PivotLandscapeThemePadding}”/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <ContentControl x:Name=”TitleContentControl” ContentTemplate=”{TemplateBinding TitleTemplate}” Content=”{TemplateBinding Title}” Style=”{StaticResource PivotTitleContentControlStyle}”/> <ScrollViewer x:Name=”ScrollViewer” HorizontalSnapPointsAlignment=”Center” HorizontalSnapPointsType=”MandatorySingle” HorizontalScrollBarVisibility=”Hidden” Margin=”{TemplateBinding Padding}” Grid.Row=”1″ Template=”{StaticResource ScrollViewerScrollBarles@R_502_2354@plate}” VerticalSnapPointsType=”None” VerticalScrollBarVisibility=”Disabled” VerticalScrollMode=”Disabled” VerticalContentAlignment=”Stretch” ZoomMode=”Disabled”> <PivotPanel x:Name=”Panel” VerticalAlignment=”Stretch”> <PivotHeaderPanel x:Name=”Header” Background=”{TemplateBinding BorderBrush}”> <PivotHeaderPanel.RenderTransform> <CompositeTransform x:Name=”HeaderTranslateTransform” TranslateX=”0″/> </PivotHeaderPanel.RenderTransform> </PivotHeaderPanel> <ItemsPresenter x:Name=”PivotItemPresenter”> <ItemsPresenter.RenderTransform> <TranslateTransform x:Name=”ItemsPresenterTranslateTransform” X=”0″/> </ItemsPresenter.RenderTransform> </ItemsPresenter> </PivotPanel> </ScrollViewer> </Grid> </ControlTemplate> </Setter.Value> </Setter></Style>

在下面找到这一行,我对默认样式的唯一变化是将Background =“{TemplateBinding BorderBrush}”添加到托管所有标题的面板的PivotHeaderPanel中.

<PivotHeaderPanel x:Name=”Header” Background=”{TemplateBinding BorderBrush}”>

我在这里使用TemplateBinding的原因是因为这样做可以通过指定数据透视的BorderBush来灵活地更改标题背景.由于BorderBrush在控件中的任何位置都不被使用,所以如果我们改变它就不会有任何副作用.

所以你所需要做的就是这个.

<Pivot x:Uid=”Pivot” Title=”MY APPLICATION” x:Name=”pivot” CommonNavigationTransitionInfo.IsStaggerElement=”True” Style=”{StaticResource CustomPivotStyle}” BorderBrush=”{StaticResource PhoneAccentBrush}”>

这就是他们现在的样子.

希望这可以帮助!