[C#/WPF] WeakEventManager<TEventSource, TEventArgs> 클래스 : AddHandler 정적 메소드를 사용해 이벤트 핸들러 추가하기
C#/WPF 2022. 1. 28. 00:24728x90
반응형
728x170
▶ Sender.cs
using System;
namespace TestProject
{
/// <summary>
/// 송신자
/// </summary>
public class Sender
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 호출시 이벤트 - Called
/// <summary>
/// 호출시 이벤트
/// </summary>
public static event EventHandler Called;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 호출하기 - Call()
/// <summary>
/// 호출하기
/// </summary>
public static void Call()
{
if(Called != null)
{
Called(null, EventArgs.Empty);
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] 수신자를 호출했습니다.");
}
}
#endregion
}
}
728x90
▶ Receiver.cs
using System;
using System.Windows;
namespace TestProject
{
/// <summary>
/// 수신자
/// </summary>
public class Receiver
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 메시지
/// </summary>
private string message;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - Receiver(message)
/// <summary>
/// 생성자
/// </summary>
/// <param name="message">메시지</param>
public Receiver(string message)
{
this.message = message;
//Sender.Called += Sender_Called;
WeakEventManager<Sender, EventArgs>.AddHandler(null, "Called", sender_Called);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Destructor
#region 소멸자 - ~Receiver()
/// <summary>
/// 소멸자
/// </summary>
~Receiver()
{
Console.WriteLine($"수신자의 소멸자가 실행되었습니다 : {this.message}");
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 송신자 호출시 처리하기 - sender_Called(sender, e)
/// <summary>
/// 송신자 호출시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void sender_Called(object sender, EventArgs e)
{
Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss")}] 송신자가 호출했습니다 : {this.message}");
}
#endregion
}
}
반응형
▶ Program.cs
using System;
namespace TestProject
{
/// <summary>
/// 프로그램
/// </summary>
class Program
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
private static void Main()
{
Receiver receiver1 = new Receiver("사과");
Receiver receiver2 = new Receiver("딸기");
Sender.Call();
receiver1 = null;
GC.Collect();
Sender.Call();
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Storyboard 엘리먼트 : 슬라이딩 패널 만들기 (0) | 2022.02.04 |
---|---|
[C#/WPF] 투명 윈도우 마우스 따라다니기 (0) | 2022.01.29 |
[C#/WPF] Visual 클래스 : PointToScreen 메소드를 사용해 마우스 화면 좌표 구하기 (0) | 2022.01.29 |
[C#/WPF] Button 엘리먼트 : FocusVisualStyle 속성을 사용해 포커스 비주얼 스타일 설정하기 (0) | 2022.01.28 |
[C#/WPF] UIElement 클래스 : GotFocus/LostFocus 이벤트를 사용해 키보드 포커스 획득/상실시 처리하기 (0) | 2022.01.28 |
[C#/WPF] EventManager 클래스 : RegisterClassHandler 정적 메소드를 사용해 특정 라우팅 이벤트에 대한 클래스 처리기 등록하기 (0) | 2022.01.27 |
[C#/WPF] ControlTemplate 엘리먼트 : Button 엘리먼트 정의하기 (0) | 2022.01.27 |
[C#/WPF] Decorator 엘리먼트 : 커스텀 버튼 크롬 만들기 (0) | 2022.01.27 |
[C#/WPF] ButtonChrome 엘리먼트 사용하기 (0) | 2022.01.27 |
[C#/WPF] 커스텀 포커스 범위 사용하기 (0) | 2022.01.27 |
댓글을 달아 주세요