WPF项目中不支持画布

本文关键字:不支持 项目 WPF | 更新日期: 2023-09-27 18:04:31

我正在尝试使用StoryboardDoubleAnimationsUsingKeyFrames在WPF中动画画布,我的代码是这样的:

<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。地铁项目。<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(bgCanvas.Opacity)" Storyboard.Target="bgCanvas">用蓝线下划线,错误如下:bgCanvas is not supported in a Windows Presentation Format (WPF) Application.

我不知道代码出了什么问题。

奖励:这是在WPF中动画画布的正确方法吗?

很抱歉有那么多问题。

WPF项目中不支持画布

你可以在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>

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