방법: 해당 Timeline의 속도를 변경하지 않고 시계의 속도 변경
ClockController 개체의 SpeedRatio 속성을 사용하면 클록 Timeline의 SpeedRatio를 변경하지 않고도 Clock의 속도를 변경할 수 있습니다. 다음 예제에서 ClockController는 시계의 SpeedRatio를 대화형으로 수정하는 데 사용됩니다. CurrentGlobalSpeedInvalidated 이벤트 및 클록의 CurrentGlobalSpeed 속성은 대화형 SpeedRatio가 변경될 때마다 시계의 현재 글로벌 속도를 표시하는 데 사용됩니다.
예제
/*
This example shows how to interactively control
the speed of a clock
*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Microsoft.Samples.Animation.TimingBehaviors
{
public class ClockControllerSpeedRatioExample : Page
{
private AnimationClock myControllableClock;
private Button speedRatioButton;
private TextBox speedRatioSettingTextBox;
private double doubleParseResult = 1;
private TextBlock currentGlobalSpeedTextBlock;
public ClockControllerSpeedRatioExample()
{
StackPanel mainPanel = new StackPanel();
// Create a rectangle to animate.
Rectangle animatedRectangle = new Rectangle();
animatedRectangle.Width = 100;
animatedRectangle.Height = 100;
animatedRectangle.Fill = Brushes.Orange;
mainPanel.Children.Add(animatedRectangle);
// Create a DoubleAnimation to
// animate its width.
DoubleAnimation widthAnimation =
new DoubleAnimation(
100,
500,
new Duration(TimeSpan.FromSeconds(5)));
//widthAnimation.RepeatBehavior = RepeatBehavior.Forever;
widthAnimation.AutoReverse = true;
widthAnimation.SpeedRatio = 0.5;
// Create a clock from the animation.
myControllableClock = widthAnimation.CreateClock();
// Apply the clock to the rectangle's Width property.
animatedRectangle.ApplyAnimationClock(
Rectangle.WidthProperty, myControllableClock);
//
// Create some controls the enable the user to
// interactively control the SpeedRatio of the clock.
//
StackPanel speedRatioDetailsPanel = new StackPanel();
speedRatioDetailsPanel.Margin = new Thickness(0,20,0,20);
speedRatioDetailsPanel.Orientation = Orientation.Horizontal;
Label speedRatioLabel = new Label();
speedRatioLabel.Content = "Speed Ratio:";
speedRatioDetailsPanel.Children.Add(speedRatioLabel);
// Create a text box so that the user can
// specify the amount by which to seek.
speedRatioSettingTextBox = new TextBox();
speedRatioSettingTextBox.Text =
myControllableClock.Controller.SpeedRatio.ToString();
speedRatioSettingTextBox.VerticalAlignment = VerticalAlignment.Top;
speedRatioSettingTextBox.TextChanged +=
new TextChangedEventHandler(seekAmountTextBox_TextChanged);
speedRatioDetailsPanel.Children.Add(speedRatioSettingTextBox);
// Create a button to apply SpeedRatio changes.
speedRatioButton = new Button();
speedRatioButton.Content = "Apply Speed Ratio";
speedRatioButton.Click += new RoutedEventHandler(speedRatioButton_Clicked);
speedRatioDetailsPanel.Children.Add(speedRatioButton);
mainPanel.Children.Add(speedRatioDetailsPanel);
// Display the clock's global speed information.
Label myLabel = new Label();
myLabel.Content = "CurrentGlobalSpeed ";
mainPanel.Children.Add(myLabel);
currentGlobalSpeedTextBlock = new TextBlock();
currentGlobalSpeedTextBlock.Text =
myControllableClock.CurrentGlobalSpeed.ToString();
mainPanel.Children.Add(currentGlobalSpeedTextBlock);
// List for speed changes.
myControllableClock.CurrentGlobalSpeedInvalidated +=
new EventHandler(myControllableClock_currentGlobalSpeedInvalidated);
this.Content = mainPanel;
}
// Updates the clock's SpeedRatio.
private void speedRatioButton_Clicked(object sender, RoutedEventArgs e)
{
// This statement triggers a CurrentGlobalSpeedInvalidated
// event.
myControllableClock.Controller.SpeedRatio = doubleParseResult;
}
// Displays the current global speed.
private void myControllableClock_currentGlobalSpeedInvalidated(object sender, EventArgs e)
{
currentGlobalSpeedTextBlock.Text =
myControllableClock.CurrentGlobalSpeed.ToString()
+ " Event triggered at: "
+ DateTime.Now.ToString();
}
// Verifies that speedRatioSettingTextBox has valid text content.
// If it doesn't, the speedRatioButton is disabled.
private void seekAmountTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox theTextBox = (TextBox)e.Source;
if (theTextBox.Text == null || theTextBox.Text.Length < 1
|| Double.TryParse(theTextBox.Text,
System.Globalization.NumberStyles.Any,
null, out doubleParseResult) == false)
speedRatioButton.IsEnabled = false;
else
speedRatioButton.IsEnabled = true;
}
}
}
'
' This example shows how to interactively control
' the speed of a clock
'
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
Namespace Microsoft.Samples.Animation.TimingBehaviors
Public Class ClockControllerSpeedRatioExample
Inherits Page
Private ReadOnly myControllableClock As AnimationClock
Private ReadOnly speedRatioButton As Button
Private ReadOnly speedRatioSettingTextBox As TextBox
Private doubleParseResult As Double = 1
Private ReadOnly currentGlobalSpeedTextBlock As TextBlock
Public Sub New()
Dim mainPanel As New StackPanel()
' Create a rectangle to animate.
Dim animatedRectangle As New Rectangle With {
.Width = 100,
.Height = 100,
.Fill = Brushes.Orange
}
mainPanel.Children.Add(animatedRectangle)
' Create a DoubleAnimation to
' animate its width.
'widthAnimation.RepeatBehavior = RepeatBehavior.Forever
Dim widthAnimation As New DoubleAnimation(100, 500, New Duration(TimeSpan.FromSeconds(5))) With {
.AutoReverse = True,
.SpeedRatio = 0.5
}
' Create a clock from the animation.
myControllableClock = widthAnimation.CreateClock()
' Apply the clock to the rectangle's Width property.
animatedRectangle.ApplyAnimationClock(Rectangle.WidthProperty, myControllableClock)
'
' Create some controls the enable the user to
' interactively control the SpeedRatio of the clock.
'
Dim speedRatioDetailsPanel As New StackPanel With {
.Margin = New Thickness(0, 20, 0, 20),
.Orientation = Orientation.Horizontal
}
Dim speedRatioLabel As New Label With {
.Content = "Speed Ratio:"
}
speedRatioDetailsPanel.Children.Add(speedRatioLabel)
' Create a text box so that the user can
' specify the amount by which to seek.
speedRatioSettingTextBox = New TextBox With {
.Text = myControllableClock.Controller.SpeedRatio.ToString(),
.VerticalAlignment = VerticalAlignment.Top
}
AddHandler speedRatioSettingTextBox.TextChanged, AddressOf seekAmountTextBox_TextChanged
speedRatioDetailsPanel.Children.Add(speedRatioSettingTextBox)
' Create a button to apply SpeedRatio changes.
speedRatioButton = New Button With {
.Content = "Apply Speed Ratio"
}
AddHandler speedRatioButton.Click, AddressOf speedRatioButton_Clicked
speedRatioDetailsPanel.Children.Add(speedRatioButton)
mainPanel.Children.Add(speedRatioDetailsPanel)
' Display the clock's global speed information.
Dim myLabel As New Label With {
.Content = "CurrentGlobalSpeed "
}
mainPanel.Children.Add(myLabel)
currentGlobalSpeedTextBlock = New TextBlock With {
.Text = myControllableClock.CurrentGlobalSpeed.ToString()
}
mainPanel.Children.Add(currentGlobalSpeedTextBlock)
' List for speed changes.
AddHandler myControllableClock.CurrentGlobalSpeedInvalidated, AddressOf myControllableClock_currentGlobalSpeedInvalidated
Content = mainPanel
End Sub
' Updates the clock's SpeedRatio.
Private Sub speedRatioButton_Clicked(sender As Object, e As RoutedEventArgs)
' This statement triggers a CurrentGlobalSpeedInvalidated
' event.
myControllableClock.Controller.SpeedRatio = doubleParseResult
End Sub
' Displays the current global speed.
Private Sub myControllableClock_currentGlobalSpeedInvalidated(sender As Object, e As EventArgs)
currentGlobalSpeedTextBlock.Text = myControllableClock.CurrentGlobalSpeed.ToString() & " Event triggered at: " & Date.Now.ToString()
End Sub
' Verifies that speedRatioSettingTextBox has valid text content.
' If it doesn't, the speedRatioButton is disabled.
Private Sub seekAmountTextBox_TextChanged(sender As Object, e As TextChangedEventArgs)
Dim theTextBox As TextBox = CType(e.Source, TextBox)
If theTextBox.Text Is Nothing OrElse theTextBox.Text.Length < 1 OrElse Double.TryParse(theTextBox.Text, System.Globalization.NumberStyles.Any, Nothing, doubleParseResult) = False Then
speedRatioButton.IsEnabled = False
Else
speedRatioButton.IsEnabled = True
End If
End Sub
End Class
End Namespace
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기