/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
const string TXT_START = "Start";
const string TXT_DISCONNECT = "Disconnect";
const string TXT_CONNECTING = "Connecting..";
const string TXT_CONNECTED = "Connected!";
const string TXT_WAITING = "Waiting..";
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.btnStartOrEnd.Content = TXT_START;
}
private void btnStartOrEnd_Click(object sender, RoutedEventArgs e)
{
if (this.btnStartOrEnd.Content.Equals(TXT_START))
{
//Start
this.TryConnect();
}
else if (this.btnStartOrEnd.Content.Equals(TXT_DISCONNECT))
{
//Disconnect
this.UpdateUIContent(TXT_START, TXT_WAITING);
}
}
/// <summary>
/// 연결을 시도합니다.
/// </summary>
private void TryConnect()
{
//버튼과 레이블의 Content를 변경하고
this.UpdateUIContent(TXT_WAITING, TXT_CONNECTING);
this.btnStartOrEnd.IsEnabled = false;
//스토리보드 시작을 하고
Storyboard sb = this.Resources["sbBlinking"] as Storyboard;
//sb.Begin(this.btnStartOrEnd); //Xaml에서 지정하지 않을 경우 cs에서 직접 지정
sb.Begin();
//연결이 3초 후 성공하였다고 가정한다.
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(new TimeSpan(0, 0, 3),
System.Windows.Threading.DispatcherPriority.SystemIdle, this.ConnectSuccess, this.Dispatcher);
}
/// <summary>
/// 연결에 성공하였습니다.
/// </summary>
/// <param name="state"></param>
//public void ConnectSuccess(object state)
public void ConnectSuccess(object sender, EventArgs e)
{
System.Windows.Threading.DispatcherTimer timer = sender as System.Windows.Threading.DispatcherTimer;
timer.Stop();
this.UpdateUIContent(TXT_DISCONNECT, TXT_CONNECTED);
Storyboard sb = this.Resources["sbBlinking"] as Storyboard;
sb.Stop();
this.btnStartOrEnd.IsEnabled = true;
}
/// <summary>
/// 상태 UI를 갱신합니다.
/// </summary>
/// <param name="btnContent"></param>
/// <param name="lblContent"></param>
private void UpdateUIContent(string btnContent, string lblContent)
{
this.btnStartOrEnd.Content = btnContent;
this.lblState.Content = lblContent;
}
}
|