728x90
반응형
728x170
■ Storyboard 엘리먼트와 BackgroundWorker 객체를 사용해 비동기 프로세스 동안 연속 애니메이션을 표시하는 방법을 보여준다.
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<Window.Resources>
<Storyboard x:Key="PulseStoryboardKey"
AutoReverse="True" >
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"
BeginTime="00:00:00">
<SplineColorKeyFrame KeyTime="00:00:00.50" Value="Lime" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames
Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"
BeginTime="00:00:00">
<SplineColorKeyFrame KeyTime="00:00:00.50" Value="Green" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Ellipse Name="ellipse"
Margin="10"
Width="400"
Height="400"
Stroke="{x:Null}">
<Ellipse.Fill>
<RadialGradientBrush GradientOrigin="0.25 0.25">
<GradientStop Offset="0" Color="Red" />
<GradientStop Offset="1" Color="Blue" />
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Button Name="startButton" Grid.Row="1"
Margin="10"
Width="100"
Height="30"
Content="Start" />
</Grid>
</Window>
▶ MainWindow.xaml.cs
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Media.Animation;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 펄스 스토리보드
/// </summary>
private Storyboard pulseStoryboard;
/// <summary>
/// 백그라운드 작업자
/// </summary>
private BackgroundWorker worker;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
this.pulseStoryboard = Resources["PulseStoryboardKey"] as Storyboard;
this.pulseStoryboard.RepeatBehavior = RepeatBehavior.Forever;
this.worker = new BackgroundWorker();
this.startButton.Click += startButton_Click;
this.worker.DoWork += worker_DoWork;
this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Start 버튼 클릭시 처리하기 - startButton_Click(sender, e)
/// <summary>
/// Start 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void startButton_Click(object sender, RoutedEventArgs e)
{
this.pulseStoryboard.Begin(this, true);
this.worker.RunWorkerAsync();
this.startButton.IsEnabled = false;
}
#endregion
#region 백그라운자 작업자 작업시 처리하기 - worker_DoWork(sender, e)
/// <summary>
/// 백그라운자 작업자 작업시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
for(int i = 1; i <= 50; i++)
{
Thread.Sleep(100);
}
}
#endregion
#region 백그라운자 작업자 작업 완료시 처리하기 - worker_RunWorkerCompleted(sender, e)
/// <summary>
/// 백그라운자 작업자 작업 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.startButton.IsEnabled = true;
this.pulseStoryboard.Stop(this);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] GridViewRowPresenter 엘리먼트 : 데이터 표시하기 (0) | 2023.01.13 |
---|---|
[C#/WPF] EventSetter 엘리먼트 : Event/Handler 속성을 사용해 ListBoxItem 객체를 마우스 더블 클릭시 처리하기 (0) | 2023.01.13 |
[C#/WPF] GridSplitter 엘리먼트 : Panel 엘리먼트의 ZIndex 첨부 속성을 사용해 그리드 스플리터 숨김 방지하기 (0) | 2023.01.12 |
[C#/WPF] GridSplitter 엘리먼트 : 그리드 행 크기 변경하기 (0) | 2023.01.12 |
[C#/WPF] DockPanel 클래스 : 공간 분할하기 (0) | 2023.01.12 |
[C#/WPF] Style 엘리먼트 : TargetType 속성을 사용해 MenuItem 엘리먼트 스타일 설정하기 (0) | 2023.01.09 |
[C#/WPF] LengthConverter 클래스 : ConvertFromString 메소드를 사용해 문자열에서 길이 구하기 (0) | 2023.01.09 |
[C#/WPF] BulletDecorator 엘리먼트 사용하기 (0) | 2023.01.09 |
[C#/WPF] Border 클래스 : BorderThickness 속성을 변경시키는 애니메이션 만들기 (0) | 2023.01.09 |
[C#/WPF] ControlTemplate 엘리먼트 : 버튼 배경색을 그라데이션 처리한 Button 엘리먼트 정의하기 (0) | 2023.01.08 |
댓글을 달아 주세요