2014-04-28 1 views
0

그래서 두 번째로 계산하는 모든 페이지에 타이머가 필요한 응용 프로그램에서 일하고 있습니다. 나는 그것이 클래스에 실제 함수를 가지고 그것을 필요로하는 페이지에 의해 호출되도록하는 것이 가장 좋을 것이라고 생각했다. 내가 아는 것은 페이지에서 타이머를 작동시키는 법입니다 ... 저에게 어떤 교묘 함이 있습니다.클래스에서 디스패처 타이머를 구현하는 방법 및

말할 필요도없이, 나는 실패하고 있습니다. 여기

내가 수업 시간에 무슨 짓을했는지의 :

namespace Masca 
    { 
    public class timer 
    { 

    public void StartTimer() 
    { 
     System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 
     dispatcherTimer.Start(); 
    } 

    private void dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     DateTime datetime = DateTime.Now; 
    } 

그리고 한 페이지에 한 일을 내가

namespace Masca 
{ 

public partial class signup : Elysium.Controls.Window 
{ 
    public timer timer; 

    public signup(string Str_Value) 
    { 

     InitializeComponent(); 
     tag.Text = Str_Value; 
    } 

    public void dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
     DateTime datetime = DateTime.Now; 
     this.doc.Text = datetime.ToString(); 
    } 

에서 타이머 나는 'dispatcherTimer_Tick을 얻을 수 없습니다 필요 '이벤트를 통해 클래스'타이머 '에서 작업하는 방법에 대한 지침을 받아야한다는 것을 알고 있습니다.

방법에 대한 아이디어가 있으십니까?

답변

1

당신은 아마 당신의 타이머 클래스에 이벤트를 추가 할 :

public class timer 
{ 

public event EventHandler TimerTick; 

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    if (TimerTick != null) 
     TimerTick(this, null); 
} 

은 그래서 당신의 창에 당신은이 이벤트를 수신 할 수 있습니다.

0

자신의 이벤트 또는 timer 클래스의 델리게이트 중 하나를 노출해야합니다. 외부 클래스는이 이벤트/위임자를 구독하고 timer 클래스의 dispatcherTimer_Tick 메서드에서 발생 시키거나 호출합니다.

나는 당신의 timer 수업 시간에 이런 짓을 할 것이다 : ...

public delegate void TimeUp(); // define delegate 

public TimeUp OnTimeUp { get; set; } // expose delegate 

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    DateTime datetime = DateTime.Now; 
    if (OnTimeUp != null) OnTimeUp(); // call delegate 
} 

그리고 클래스 외부에서

:

public timer timer; 

...

timer.OnTimeUp += timerOnTimeUp; 

private void timerOnTimeUp() 
{ 
    // time is up 
} 
관련 문제