如何在 Windows Phone 8 的 app.xaml 中应用全局转换规则

Posted

技术标签:

【中文标题】如何在 Windows Phone 8 的 app.xaml 中应用全局转换规则【英文标题】:How to apply global transform rules in app.xaml in windows phone 8 【发布时间】:2015-04-15 20:36:28 【问题描述】:

我想将此转换规则应用于所有复选框:

  <CheckBox.RenderTransform>
        <ScaleTransform ScaleX="0.7" ScaleY="0.7" />
  </CheckBox.RenderTransform>

有什么方法可以将它放入 app.xaml 中,这样我就不需要将这条规则写入每个复选框?

【问题讨论】:

不,唯一的解决方案是创建您自己的复选框用户控件,然后对所有 xaml 文件运行搜索和替换,以将系统文件替换为您的。 好的,谢谢@StuartSmith 【参考方案1】:

当然,您所要做的就是覆盖默认的复选框样式。将此代码添加到您的 App.xaml 文件中的&lt;Application.Resources&gt; 部分:

<!--Application Resources-->
<Application.Resources>
    <Style x:Key="PhoneButtonBase" TargetType="ButtonBase">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="StaticResource PhoneForegroundBrush"/>
        <Setter Property="Foreground" Value="StaticResource PhoneForegroundBrush"/>
        <Setter Property="BorderThickness" Value="StaticResource PhoneBorderThickness"/>
        <Setter Property="FontFamily" Value="StaticResource PhoneFontFamilySemiBold"/>
        <Setter Property="FontSize" Value="StaticResource PhoneFontSizeMediumLarge"/>
        <Setter Property="Padding" Value="10,3,10,5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ButtonBase">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneBackgroundBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneForegroundBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneForegroundBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Background="TemplateBinding Background" CornerRadius="0" Margin="StaticResource PhoneTouchTargetOverhang">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="TemplateBinding ContentTemplate" Content="TemplateBinding Content" Foreground="TemplateBinding Foreground" HorizontalContentAlignment="TemplateBinding HorizontalContentAlignment" Padding="TemplateBinding Padding" VerticalContentAlignment="TemplateBinding VerticalContentAlignment"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="PhoneRadioButtonCheckBoxBase" BasedOn="StaticResource PhoneButtonBase" TargetType="ToggleButton">
        <Setter Property="Background" Value="StaticResource PhoneRadioCheckBoxBrush"/>
        <Setter Property="BorderBrush" Value="StaticResource PhoneRadioCheckBoxBrush"/>
        <Setter Property="FontSize" Value="StaticResource PhoneFontSizeMedium"/>
        <Setter Property="FontFamily" Value="StaticResource PhoneFontFamilyNormal"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0"/>
    </Style>
    <Style BasedOn="StaticResource PhoneRadioButtonCheckBoxBase" TargetType="CheckBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <Grid Background="Transparent">
                        <Grid.RenderTransform>
                            <ScaleTransform ScaleX="0.7" ScaleY="0.7" />
                        </Grid.RenderTransform>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="CheckBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxPressedBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="CheckBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxPressedBorderBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckMark">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxCheckBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="IndeterminateMark">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxCheckBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="CheckBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="CheckBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckMark">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxCheckDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="IndeterminateMark">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneRadioCheckBoxCheckDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource PhoneDisabledBrush"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="CheckMark">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked"/>
                                <VisualState x:Name="Indeterminate">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="IndeterminateMark">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid Margin="StaticResource PhoneTouchTargetLargeOverhang">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="32"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Border x:Name="CheckBackground" BorderBrush="TemplateBinding Background" BorderThickness="StaticResource PhoneBorderThickness" Background="TemplateBinding Background" HorizontalAlignment="Left" Height="32" IsHitTestVisible="False" VerticalAlignment="Center" Width="32"/>
                            <Rectangle x:Name="IndeterminateMark" Fill="StaticResource PhoneRadioCheckBoxCheckBrush" HorizontalAlignment="Center" Height="16" IsHitTestVisible="False" Grid.Row="0" Visibility="Collapsed" VerticalAlignment="Center" Width="16"/>
                            <Path x:Name="CheckMark" Data="M0,119 L31,92 L119,185 L267,0 L300,24 L122,250 z" Fill="StaticResource PhoneRadioCheckBoxCheckBrush" HorizontalAlignment="Center" Height="18" IsHitTestVisible="False" Stretch="Fill" StrokeThickness="2" StrokeLineJoin="Round" Visibility="Collapsed" VerticalAlignment="Center" Width="24"/>
                            <ContentControl x:Name="ContentContainer" ContentTemplate="TemplateBinding ContentTemplate" Content="TemplateBinding Content" Grid.Column="1" Foreground="TemplateBinding Foreground" HorizontalContentAlignment="TemplateBinding HorizontalContentAlignment" Margin="12,0,0,0" Padding="TemplateBinding Padding" VerticalContentAlignment="TemplateBinding VerticalContentAlignment"/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

我在复选框模板中添加了比例转换:

<Grid.RenderTransform>
    <ScaleTransform ScaleX="0.7" ScaleY="0.7" />
</Grid.RenderTransform>

我知道,这是很多 XAML 代码。但是您可以在此处轻松更改复选框控件的任何方面。

【讨论】:

以上是关于如何在 Windows Phone 8 的 app.xaml 中应用全局转换规则的主要内容,如果未能解决你的问题,请参考以下文章

windows phone silverlight 8 app 系统托盘颜色变化

如何发送推送通知 windows phone 8 应用程序?

[Windows Phone 8]如何启动浏览器并指定其网址WebBrowserTask

c# Windows Phone 8.1 如何获取地理坐标对象

如何在 Windows Phone 8 应用程序中播放循环背景音频(不使用 BackroundAudio 服务)?

如何从我的 windows phone 8 应用程序调用 yandex navigator?