c# - 使用 WPF C# 同时对两个 UIElement 进行动画处理

标签 c# wpf animation asynchronous storyboard

我这里有两个动画,一个用于关闭滑出网格导航菜单,另一个用于在菜单关闭时将矩形填充设置为透明。

我希望这两者同时发生。现在动画按照调用的顺序发生。

如何使用 C# 代码尽可能简单、干净地实现此功能?我创建此应用程序只是为了了解动画和布局控制的不同方式。

private void _CloseSlideGrid()
{
    DoubleAnimation da = new DoubleAnimation();
    da.Duration = TimeSpan.FromSeconds(0.3d);
    da.DecelerationRatio = 1.0d;
    da.From = 500.0d;
    da.To = 0.0d;
    _slideGrid.BeginAnimation(Grid.WidthProperty, da);
}

private void _DisableTransparentCover()
{
    BrushAnimation ba = new BrushAnimation();
    ba.Duration = TimeSpan.FromSeconds(0.3d);
    ba.DecelerationRatio = 1.0d;
    ba.From = _GetBrush("#77000000");
    ba.To = _GetBrush("#00000000");
    _tranparentCover.BeginAnimation(Rectangle.FillProperty, ba);
}

关闭按钮的事件回调会调用两个处理动画的私有(private)函数。

private void _NavCloseButton_Click(object sender, RoutedEventArgs e)
{
    _CloseSlideGrid();
    _DisableTransparentCover();
}

如果您有兴趣,这里有一个 Imgur 相册的链接,其中包含我的窗口的两种状态的屏幕截图: /image/UEubH.jpg

如果我可以提供更多信息,请告诉我,

谢谢。

最佳答案

只需将它们添加到同一个 Storyboard上就可以了。我不确定您的 BrushAnimation 是什么,但使用 ColorAnimation 和如下属性路径工作得很好。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        SizeToContent="WidthAndHeight">
    <Window.Resources>
        <Storyboard x:Key="CloseAnimation">
            <DoubleAnimation From="500.0" To="0.0" DecelerationRatio="1.0" Duration="00:00:03"
                             Storyboard.TargetName="MyTextBox" Storyboard.TargetProperty="Width"/>
            <ColorAnimation From="#77000000" To="#00000000" DecelerationRatio="1.0" Duration="00:00:03"
                            Storyboard.TargetName="MyGrid" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"/>
        </Storyboard>
    </Window.Resources>

    <StackPanel>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <TextBox x:Name="MyTextBox" Grid.Column="0" Width="500"/>

            <Grid x:Name="MyGrid" Grid.Column="1" Background="#77000000" Width="100"/>
        </Grid>

        <Button Content="Animate">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click" >
                    <BeginStoryboard  Storyboard="{StaticResource CloseAnimation}"/>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </StackPanel>
</Window>

如果您真的想在代码后面执行此操作并且没有其他方法,则可以这样翻译。

private void _CloseSlideGrid(Storyboard sb)
{
    DoubleAnimation da = new DoubleAnimation();
    da.Duration = TimeSpan.FromSeconds(0.3d);
    da.DecelerationRatio = 1.0d;
    da.From = 500.0d;
    da.To = 0.0d;
    Storyboard.SetTarget(da, MyTextBox);
    Storyboard.SetTargetProperty(da, new PropertyPath("Width"));

    sb.Children.Add(da);
}

private void _DisableTransparentCover(Storyboard sb)
{
    ColorAnimation ba = new ColorAnimation();
    ba.Duration = TimeSpan.FromSeconds(0.3d);
    ba.DecelerationRatio = 1.0d;
    ba.From = (Color)ColorConverter.ConvertFromString("#77000000");
    ba.To = (Color)ColorConverter.ConvertFromString("#00000000");

    Storyboard.SetTarget(ba, MyGrid);
    Storyboard.SetTargetProperty(ba, new PropertyPath("(Background).(SolidColorBrush.Color)"));

    sb.Children.Add(ba);
}

private void _NavCloseButton_Click(object sender, RoutedEventArgs e)
{
    var sb = new Storyboard();

    _CloseSlideGrid(sb);
    _DisableTransparentCover(sb);

    sb.Begin();
}

关于c# - 使用 WPF C# 同时对两个 UIElement 进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34804893/

相关文章:

c# - WebRequest.Create 不正确地解码 url

c# - WPF Key枚举有什么解释吗?

android - 隐藏/显示操作栏更流畅?

c# - 使用自引用表更新 LINQ 记录时出现 StackOverFlowException

c# - 从点和关系形成三角形

wpf - 母版页但针对区域?

c# - 组合框数据触发器

javascript - 打开上面的div时使div向下滑动(不打开)

html - 悬停在图标上后,如何流畅显示带文字的背景色?

c# - 如何引用引用类型字典键的属性?