XamlWriter跳过" x:名字"保存ResourceDictionary时的属性

时间:2015-02-13 12:50:25

标签: wpf xaml wpf-style xamlwriter

这里的自定义风格:

<Style TargetType="{x:Type Button}">
    <Setter Property="Focusable" Value="false" />
    <Setter Property="Background" Value="{StaticResource AppBackBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource AppBrush}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="{StaticResource AppBrush}"
                        Name="content"
                        BorderThickness="1"
                        CornerRadius="3"
                        Background="{StaticResource AppBackBrush}"
                        >
                    <Grid Background="Transparent">
                        <Label Content="{TemplateBinding Content}"
                               HorizontalContentAlignment="Center"
                               VerticalContentAlignment="Center"
                               Grid.Row="0" Grid.Column="0"
                               Background="Transparent"
                               Style="{x:Null}"
                               Foreground="{TemplateBinding Foreground}"
                               Padding="{TemplateBinding Padding}"/>
                    </Grid>
                    <Border.RenderTransform>
                        <!-- push the content a bit to the left and the top -->
                        <TranslateTransform x:Name="translation"
                                            X="-1" Y="-1"/>
                    </Border.RenderTransform>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:0"
                                                     To="0"
                                                     Storyboard.TargetName="translation"
                                                     Storyboard.TargetProperty="(TranslateTransform.X)"/>
                                    <DoubleAnimation Duration="0:0:0"
                                                     To="0"
                                                     Storyboard.TargetName="translation"
                                                     Storyboard.TargetProperty="(TranslateTransform.Y)"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:0"
                                                     To="-1"
                                                     Storyboard.TargetName="translation"
                                                     Storyboard.TargetProperty="(TranslateTransform.X)"/>
                                    <DoubleAnimation Duration="0:0:0"
                                                     To="-1"
                                                     Storyboard.TargetName="translation"
                                                     Storyboard.TargetProperty="(TranslateTransform.Y)"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="content" Property="Opacity" Value="0.5" />
                    </Trigger>

                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我将包含此样式的ResourceDictionary保存为字符串,如下所示:

XamlWriter.Save(s);

其中s是ResourceDictionary。 问题是当我得到预期的字符串时,它看起来如此:

<Style TargetType=\"Button\" x:Key=\"{x:Type Button}\">
    <Style.Resources>
        <ResourceDictionary />
    </Style.Resources>
    <Setter Property=\"UIElement.Focusable\">
        <Setter.Value>
            <s:Boolean>False</s:Boolean>
        </Setter.Value>
    </Setter>
    <Setter Property=\"Panel.Background\">
        <Setter.Value>
            <SolidColorBrush>#FFF1F2F4</SolidColorBrush>
        </Setter.Value>
    </Setter>
    <Setter Property=\"TextElement.Foreground\">
        <Setter.Value>
            <SolidColorBrush>#FF13776A</SolidColorBrush>
        </Setter.Value>
    </Setter>
    <Setter Property=\"Control.Template\">
        <Setter.Value>
            <ControlTemplate TargetType=\"Button\">
                <Border BorderThickness=\"1,1,1,1\" CornerRadius=\"3,3,3,3\" BorderBrush=\"#FF13776A\" Background=\"#FFF1F2F4\" Name=\"content\">
                    <Border.RenderTransform>
                        <TranslateTransform X=\"-1\" Y=\"-1\" />
                    </Border.RenderTransform>
                    <Grid>
                        <Grid.Style>
                            <Style TargetType=\"Grid\">
                                <Style.Resources>
                                    <ResourceDictionary />
                                </Style.Resources>
                                <Setter Property=\"Panel.Background\">
                                    <Setter.Value>
                                        <SolidColorBrush>#00FFFFFF</SolidColorBrush>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Grid.Style>
                        <Label Content=\"{TemplateBinding ContentControl.Content}\" Background=\"#00FFFFFF\" Foreground=\"{TemplateBinding TextElement.Foreground}\" HorizontalContentAlignment=\"Center\" VerticalContentAlignment=\"Center\" Padding=\"{TemplateBinding Control.Padding}\" Style=\"{x:Null}\" Grid.Column=\"0\" Grid.Row=\"0\" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property=\"ButtonBase.IsPressed\">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <Storyboard.Children>
                                        <DoubleAnimation To=\"0\" Duration=\"00:00:00\" Storyboard.TargetName=\"translation\" Storyboard.TargetProperty=\"(TranslateTransform.X)\" />
                                        <DoubleAnimation To=\"0\" Duration=\"00:00:00\" Storyboard.TargetName=\"translation\" Storyboard.TargetProperty=\"(TranslateTransform.Y)\" />
                                    </Storyboard.Children>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <Storyboard.Children>
                                <DoubleAnimation To=\"-1\" Duration=\"00:00:00\" Storyboard.TargetName=\"translation\" Storyboard.TargetProperty=\"(TranslateTransform.X)\" />
                                <DoubleAnimation To=\"-1\" Duration=\"00:00:00\" Storyboard.TargetName=\"translation\" Storyboard.TargetProperty=\"(TranslateTransform.Y)\" />
                            </Storyboard.Children>
                        </Storyboard>
                    </BeginStoryboard>
                    </Trigger.ExitActions>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property=\"UIElement.IsEnabled\">
                        <Setter Property=\"UIElement.Opacity\" TargetName=\"content\">
                            <Setter.Value>
                                <s:Double>0.5</s:Double>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

请参阅 Border.RenderTransform 中的 TranslateTransform 。在ResourceDictionary中它有x:Name =&#34; translation&#34;,但输出字符串中缺少名称。

我错在哪里或者这是一个错误?提前谢谢。

1 个答案:

答案 0 :(得分:0)

根据this MSDN blog post,&#34; 一些标记扩展,例如{x:Static},在加载时被XamlReader解析,标记扩展本身被丢弃,所以没有意味着XamlWriter可以重新生成它。&#34;看起来x:Name是丢失的标记扩展之一。

看起来你正在尝试创建两个样式 - 原始样式和扩展样式 - 两者都隐式地定位按钮(可能在不同的范围内)。如果是这种情况,您可以在上面可以引用的样式的资源中创建基本样式,然后使用样式的BasedOn属性来防止复制模板。