【问题标题】:Set TextBlock Foreground via Storyboard in Universal Windows doesn't work在通用 Windows 中通过情节提要设置 TextBlock 前景不起作用
【发布时间】:2015-12-12 05:53:39
【问题描述】:

我尝试使用 Storyboard 为 TextBlock 前景色设置动画。它在 WPF 中完美运行,但在通用 Windows 中似乎无法运行。

这是我的代码:

<Page
    x:Class="TestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <TextBlock x:Name="tbHello" Text="Hello World" FontSize="60" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock.Foreground>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"
                ColorInterpolationMode="ScRgbLinearInterpolation">
                    <GradientStop Color="#ff666666" Offset="-0.2" />
                    <GradientStop Color="#ffffffff" Offset="-0.1" />
                    <GradientStop Color="#ff666666" Offset="0" />
                </LinearGradientBrush>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger>
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever">
                            <DoubleAnimation Storyboard.TargetName="tbHello"
                                             Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[0].(GradientStop.Offset)"
                                             From="-0.2" To="1.5" Duration="0:0:1.5" />
                            <DoubleAnimation Storyboard.TargetName="tbHello"
                                             Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[1].(GradientStop.Offset)"
                                             From="-0.1" To="1.6" Duration="0:0:1.5" />
                            <DoubleAnimation Storyboard.TargetName="tbHello"
                                             Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[2].(GradientStop.Offset)"
                                             From="0" To="1.7" Duration="0:0:1.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>

    </Grid>
</Page>

知道为什么它不能在通用 Windows 平台上运行吗?

谢谢

【问题讨论】:

    标签: c# xaml winrt-xaml win-universal-app


    【解决方案1】:

    您定义的动画被视为depended animation,所以我已经尝试过,如果您设置EnableDependentAnimation="True",它会起作用,尽管正如MSDN所说-谨慎使用它。

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock x:Name="tbHello" Text="Hello World" FontSize="60" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock.Foreground>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"
            ColorInterpolationMode="ScRgbLinearInterpolation">
                    <GradientStop Color="#ff666666" Offset="-0.2" />
                    <GradientStop Color="#ffffffff" Offset="-0.1" />
                    <GradientStop Color="#ff666666" Offset="0" />
                </LinearGradientBrush>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger>
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever">
                            <DoubleAnimation Storyboard.TargetName="tbHello" EnableDependentAnimation="True"
                                         Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[0].(GradientStop.Offset)"
                                         From="-0.2" To="1.5" Duration="0:0:1.5" />
                            <DoubleAnimation Storyboard.TargetName="tbHello" EnableDependentAnimation="True"
                                         Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[1].(GradientStop.Offset)"
                                         From="-0.1" To="1.6" Duration="0:0:1.5" />
                            <DoubleAnimation Storyboard.TargetName="tbHello" EnableDependentAnimation="True"
                                         Storyboard.TargetProperty="(TextElement.Foreground).(LinearGradientBrush.GradientStops)[2].(GradientStop.Offset)"
                                         From="0" To="1.7" Duration="0:0:1.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
    </Grid>
    

    【讨论】:

    • 你是冠军!谢谢罗马兹!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多