c# - 激活 Storyboard所需的丑陋 C# WPF 代码

标签 c# wpf xaml

为了激活我通过 XAML 声明的 Storyboard ,我发现我编写了很多以下丑陋的代码序列;

(SomeElement.TryFindResource("SomeStoryboard") as Storyboard)?.Begin();

我真正喜欢的只是写作;

SomeStoryboard.Begin();

有什么方法可以更直接地声明/处理 Storyboard 吗?这将有助于代码的可读性,并且还允许编译时而不是运行时从 C# 解析对象

最佳答案

对于简单的场景,您应该使用Trigger/DataTrigger,而不是直接管理 Storyboard。这是一个例子:

<DataTrigger Binding="{Binding IsAnimationEnabled}" Value="True">
    <DataTrigger.EnterActions>
        <BeginStoryboard x:Name="MyStoryBoard" Storyboard="{StaticResource MyStoryBoardResourceKey}"/>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <RemoveStoryboard BeginStoryboardName="MyStoryBoard"/>
    </DataTrigger.ExitActions>
</DataTrigger>

IsAnimationEnabled 是您的 viemodel(或 View )上的一些 Boolean 属性,您可以通过代码设置它以启用或禁用动画。

关于c# - 激活 Storyboard所需的丑陋 C# WPF 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615592/

相关文章:

c# - 如何从我的 C# 应用程序发送电子邮件?

c# - HttpContext 和 HttpRequest 之间的区别?

WPF Expander 控件 - 已转换的 header 宽度问题

c# - RibbonControl 正在拉伸(stretch)图像

xaml - 本地化 XAML 中的类型参数(例如 x :String)

c# - 什么时候使用 NotSupportedException 不好?

c# - .Net 多个客户端和中央服务器之间的数据同步

c# - Caliburn 微 : DialogResult

c# - 使用 WPF 和 SlimDx (DirectX 10/11)

c# - Xamarin 表格 : Image height request ignored inside frame inside relative layout