从C#触发故事板? [英] Trigger a Storyboard from C#?

查看:65
本文介绍了从C#触发故事板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在< Window.Resources>中有一个故事板。在XAML中声明并从声明的路由事件中很好地触发。

如何从C#开始讨论故事板?
我正在与FindResource()和要获取的资源的密钥名称搏斗对故事板的引用,但不是成功。

非常感谢任何工作代码的链接或片段。

I have a storyboard in <Window.Resources> declared in XAML and triggering nicely from a declared routed event.

How do I begin hte storyboard from C#?
I'm wrestling with FindResource() and the key name of the resource to get a reference to the storyboard, but not with success.

Any links or snippets to working code will be greatly appreciated.

 

谢谢。

推荐答案

在这里,请记住这没有错误检查,不应该按原样使用: )

XAML:

Here you go, but please remember this has no error checking, and should not be used as is :)

XAML:
<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="WpfApplication1.MainWindow"
	x:Name="Window"
	Title="MainWindow"
	Width="640" Height="480">
	<Window.Resources>
		<Storyboard x:Key="Storyboard1">
			<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
				<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
				<SplineDoubleKeyFrame KeyTime="00:00:01.9000000" Value="173"/>
			</DoubleAnimationUsingKeyFrames>
			<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
				<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
				<SplineDoubleKeyFrame KeyTime="00:00:01.9000000" Value="140"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
	</Window.Resources>
	<Window.Triggers>
		<EventTrigger RoutedEvent="FrameworkElement.Loaded">
			<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
		</EventTrigger>
	</Window.Triggers>

	<Grid x:Name="LayoutRoot">
		<Button x:Name="button" HorizontalAlignment="Left" Margin="84,76,0,0" VerticalAlignment="Top" Width="76" Height="39" Content="Button" RenderTransformOrigin="0.5,0.5">
			<Button.RenderTransform>
				<TransformGroup>
					<ScaleTransform/>
					<SkewTransform/>
					<RotateTransform/>
					<TranslateTransform/>
				</TransformGroup>
			</Button.RenderTransform>
		</Button>
	</Grid>
</Window>


C#

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			this.InitializeComponent();

			// Insert code required on object creation below this point.
			this.Loaded += new System.Windows.RoutedEventHandler(MainWindow_Loaded);
		}

		private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
            var sb = this.Resources["Storyboard1"] as System.Windows.Media.Animation.Storyboard;
            sb.Begin();
		}
	}
}


这篇关于从C#触发故事板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆