Instance 1:
When I got up in the morning, I had nothing to worry about, so I thought of a teacher from the College asking him to find a countdown software. At that time, I was too busy reviewing, so I was too lazy to take care of it, region ~. Since there is nothing to do in the morning, why not write a play ~ If you want to write, you can use a WPF that has never been used in the past. It is also a preliminary study of WPF (I feel very backward )!
After a long time between Vs2008 and Vs2010, I finally chose Vs2008 for IDE development. After creating a WPF project in Vs2008, you can browse the default project file structure, an App. xaml (and of course App. xaml. cs) and a Windows1.xaml (Windows1.xaml. cs ). The design interface is also different from the previous Window Form program. It feels similar to Flex, it is not supported to drag the interface design directly to the specified position (thanks to cesium and Muse for pointing out the problem for me). It is a little uncomfortable ~
Now, let's start a simple countdown timer. Let's take a look at the running effect, which is displayed in the center of the screen and on the top of the screen:
Because it is relatively simple, the three files have been written, and the MainWin. xaml designed for the interface and the application class App. xaml and countdown processing class ProcessCount. cs files are respectively written. The Code is as follows:
Countdown processing class ProcessCount. cs:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace CountDown
{
/// <Summary>
/// Class that implements the countdown Function
/// </Summary>
Public class ProcessCount
{
Private Int32 _ TotalSecond;
Public Int32 TotalSecond
{
Get {return _ TotalSecond ;}
Set {_ TotalSecond = value ;}
}
/// <Summary>
/// Constructor
/// </Summary>
Public ProcessCount (Int32 totalSecond)
{
This. _ TotalSecond = totalSecond;
}
/// <Summary>
/// Subtract seconds
/// </Summary>
/// <Returns> </returns>
Public bool ProcessCountDown ()
{
If (_ TotalSecond = 0)
Return false;
Else
{
_ TotalSecond --;
Return true;
}
}
/// <Summary>
/// Obtain the hourly display value
/// </Summary>
/// <Returns> </returns>
Public string GetHour ()
{
Return String. Format ("{0: D2}", (_ TotalSecond/3600 ));
}
/// <Summary>
/// Obtain the minute display value
/// </Summary>
/// <Returns> </returns>
Public string GetMinute ()
{
Return String. Format ("{0: D2}", (_ TotalSecond % 3600)/60 );
}
/// <Summary>
/// Obtain the second display value
/// </Summary>
/// <Returns> </returns>
Public string GetSecond ()
{
Return String. Format ("{0: D2}", _ TotalSecond % 60 );
}
}
}
Window Interface Design file MainWin. xaml:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 <Window x: Class = "CountDown. MainWin"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" Height = "400" Width = "800" HorizontalAlignment = "Center" verticalignment = "Center"
Title = "" Topmost = "True" WindowStyle = "None" Background = "Transparent" AllowsTransparency = "True" WindowStartupLocation = "CenterScreen">
<Grid>
<Grid. ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width = "40"/>
<ColumnDefinition/>
<ColumnDefinition Width = "40"/>
<ColumnDefinition/>
</Grid. ColumnDefinitions>
<TextBlock Text = "00" Name = "HourArea" verticalignment = "Center" FontSize = "180" Background = "Red" Grid. Column = "0"/>
<TextBlock Text = ":" Name = "HourSplitMinute" VerticalAlignment = "Center" FontSize = "180" Background = "Red" Grid. Column = "1"/>
<TextBlock Text = "10" Name = "MinuteArea" verticalignment = "Center" FontSize = "180" Background = "Red" Grid. Column = "2"/>
<TextBlock Text = ":" Name = "MinuteSplitSecond" VerticalAlignment = "Center" FontSize = "180" Background = "Red" Grid. Column = "3"/>
<TextBlock Text = "00" Name = "SecondArea" verticalignment = "Center" FontSize = "180" Background = "Red" Grid. Column = "4"/>
</Grid>
</Window>
Window interface logic design file: MainWin. xaml. cs:
Copy codeThe Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> 1 using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Data;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Shapes;
Using System. Windows. Threading;
Namespace CountDown
{
/// <Summary>
/// Interaction logic for MainWin. xaml
/// </Summary>
Public partial class MainWin: Window
{
Private DispatcherTimer timer;
Private ProcessCount processCount;
Public MainWin ()
{
InitializeComponent ();
This. Loaded + = new RoutedEventHandler (MainWin_Loaded );
}
/// <Summary>
/// Window loading event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void MainWin_Loaded (object sender, RoutedEventArgs e)
{
// Set the timer
Timer = new DispatcherTimer ();
Timer. Interval = new TimeSpan (10000000); // The Interval is one second.
Timer. Tick + = new EventHandler (timer_Tick );
// Convert to seconds
Int32 hour = Convert. ToInt32 (HourArea. Text );
Int32 minute = Convert. ToInt32 (MinuteArea. Text );
Int32 second = Convert. ToInt32 (SecondArea. Text );
// Process the countdown class
ProcessCount = new ProcessCount (hour * 3600 + minute * 60 + second );
CountDown + = new CountDownHandler (processCount. ProcessCountDown );
// Enable the timer
Timer. Start ();
}
/// <Summary>
/// Events triggered by Timer
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void timer_Tick (object sender, EventArgs e)
{
If (OnCountDown ())
{
HourArea. Text = processCount. GetHour ();
MinuteArea. Text = processCount. GetMinute ();
SecondArea. Text = processCount. GetSecond ();
}
Else
Timer. Stop ();
}
/// <Summary>
/// Process the event
/// </Summary>
Public event CountDownHandler CountDown;
Public bool OnCountDown ()
{
If (CountDown! = Null)
Return CountDown ();
Return false;
}
}
/// <Summary>
/// Process the countdown delegate
/// </Summary>
/// <Returns> </returns>
Public delegate bool CountDownHandler ();
}
Given that the comments in the Code are more detailed, I will not go into details one by one, hoping to help you. Download the complete project package: http://xiazai.jb51.net/201212/yuanma/countdown_jb51.rar.
Instance 2:
Effect:
UI: place a Label ---> <Label Name = "lblSecond" FontSize = "20" Foreground = "Red"> </Label>
CS:
Copy codeThe Code is as follows:
Private int countSecond = 300; // record seconds
Private void UserControl_Loaded (object sender, RoutedEventArgs e)
{
Private DispatcherTimer distmer = new DispatcherTimer ();
DisTimer. Interval = new TimeSpan (0, 0, 0, 1); // The parameters are day, hour, minute, and second, respectively. This method is overloaded and can be called as needed.
DisTimer. Tick + = new EventHandler (disTimer_Tick); // Method for executing each second
DisTimer. Start ();
}
Void disTimer_Tick (object sender, EventArgs e)
{
If (countSecond = 0)
{
MessageBox. Show ("end ");
}
Else
{
// Determine whether the lblSecond is in the UI thread
If (lblSecond. Dispatcher. CheckAccess ())
{
LblSecond. Content = countSecnd. ToString ();
}
Else
{
LblSecond. Dispatcher. BeginInvoke (DispatcherPriority. Normal, (Action) () => {
LblSecond. Content = countSecond. ToString ();
}));
}
CountSecond --;
}
}