2009-12-16 3 views
0

내가하고 싶었던 것은 내 5 살짜리 딸에게 숫자가 화면에을 포함 할 수 있음을 보여 주기만하면됩니다.WPF의 화면에서 앞으로 계산할 숫자를 얻으려면 어떻게해야합니까?

135 초를 기다린 후 "135"를 표시합니다.

숫자를 표시 할 때 변경하려면 무엇을해야합니까?

XAML :

<Window x:Class="TestCount234.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="768" Width="1024"> 
    <StackPanel> 
     <TextBlock 
      HorizontalAlignment="Center" 
      FontSize="444" x:Name="TheNumber"/> 
    </StackPanel> 
</Window> 

코드 뒤에 :

using System.Windows; 
using System.Threading; 

namespace TestCount234 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      Loaded += new RoutedEventHandler(Window1_Loaded); 
     } 

     void Window1_Loaded(object sender, RoutedEventArgs e) 
     { 
      for (int i = 0; i <= 135; i++) 
      { 
       TheNumber.Text = i.ToString(); 
       Thread.Sleep(1000); 
      } 
     } 
    } 
} 

답변

3

, 당신은 타이머를 사용할 수 있습니다 :

private DispatcherTimer timer; 
private int count = 0; 

public Window1() 
{ 
    InitializeComponent(); 
    this.timer = new DispatcherTimer(); 
    this.timer.Interval = TimeSpan.FromSeconds(1); 
    this.timer.Tick += new EventHandler(timer_Tick); 
    this.timer.Start(); 
} 

void timer_Tick(object sender, EventArgs e) 
{ 
    this.textBox1.Text = (++count).ToString(); 
} 
+0

덕분에, 작동하고, 내 여기

는 그것이 작동하는 방법의 예 5 세의 관심 스팬, 감사합니다! –

2

당신은 UI가 사용자의 작업이 실행되는 동안, 당신은 사용할 필요가 업데이트 (및 대응 유지)하려는 경우 예를 들어 BackgroundWorker을 사용하여 별도의 스레드.

BackgroundWorker _backgroundWorker = new BackgroundWorker(); 

... 

// Set up the Background Worker Events 
_backgroundWorker.DoWork += _backgroundWorker_DoWork; 
backgroundWorker.RunWorkerCompleted += 
    _backgroundWorker_RunWorkerCompleted; 

// Run the Background Worker 
_backgroundWorker.RunWorkerAsync(5000); 

... 

// Worker Method 
void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    // Do something 
} 

// Completed Method 
void _backgroundWorker_RunWorkerCompleted(
    object sender, 
    RunWorkerCompletedEventArgs e) 
{ 
    if (e.Cancelled) 
    { 
     statusText.Text = "Cancelled"; 
    } 
    else if (e.Error != null) 
    { 
     statusText.Text = "Exception Thrown"; 
    } 
    else 
    { 
     statusText.Text = "Completed"; 
    } 
} 
이 같은 급히 프로젝트

You can read a lot more about it here.

관련 문제