Canvas WPF 项目不支持?

Canvas not suppported in WPF project?

我正在尝试使用 StoryboardDoubleAnimationsUsingKeyFrames 在 WPF 中为 canvas 设置动画我的代码是这样的:

<Canvas x:Name="bgCanvas" Height="261" Canvas.Top="-262" Width="720">
            <Canvas.Background>
                <ImageBrush ImageSource="Resources/backgroundBlurred.png" Stretch="UniformToFill"/>
            </Canvas.Background>
            <Canvas.Resources>
                <Storyboard x:Key="bgAnim" x:Name="bgAnim">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(bgCanvas.Opacity)" Storyboard.Target="bgCanvas">
                        <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                        <EasingDoubleKeyFrame KeyTime="1.5" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </Canvas.Resources>

我还在项目中使用 Mahapps.Metro。 <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(bgCanvas.Opacity)" Storyboard.Target="bgCanvas"> 下划线为蓝线,错误为: bgCanvas is not supported in a Windows Presentation Format (WPF) Application.

我不确定代码有什么问题。

奖励:这是在 WPF 中为 canvas 设置动画的正确方法吗?

抱歉提出问题。

(bgCanvas.Opacity)替换为(Canvas.Opacity)

您可以在 Canvas 的 Loaded 事件的 EventTrigger 中启动动画:

<Canvas ...>
    <Canvas.Background>
        <ImageBrush .../>
    </Canvas.Background>
    <Canvas.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                        <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                        <EasingDoubleKeyFrame KeyTime="0:0:1.5" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Canvas.Triggers>
</Canvas>