最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF制作一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器實(shí)例附源碼

 更新時(shí)間:2012年12月27日 14:55:34   作者:  
既然早上沒(méi)事干,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件;何不寫(xiě)個(gè)玩玩~既然要寫(xiě),就用以前沒(méi)怎么搗鼓過(guò)的WPF寫(xiě)一個(gè)倒計(jì)時(shí)器,需要了解的朋友可以參考下
實(shí)例一
早上起來(lái)后閑的無(wú)事,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件,當(dāng)時(shí)大家忙于復(fù)習(xí)所以也懶得搭理這件事,囧~。既然早上沒(méi)事干,何不寫(xiě)個(gè)玩玩~既然要寫(xiě),就用以前沒(méi)怎么搗鼓過(guò)的WPF寫(xiě)一個(gè)吧,也算是一次學(xué)習(xí)WPF的初探吧(感覺(jué)自己很落后了)!

在Vs2008和Vs2010之間徘徊了許久之后,最終還是選擇了Vs2008做開(kāi)發(fā)IDE。在Vs2008中建了個(gè)WPF工程后,瀏覽了下默認(rèn)生成的工程文件結(jié)構(gòu),一個(gè)App.xaml(當(dāng)然還有App.xaml.cs)和一個(gè)Windows1.xaml(Windows1.xaml.cs)。設(shè)計(jì)界面也和之前的Window Form程序大不一樣了,感覺(jué)和Flex差不多,不支持直接拖拽到指定位置的界面設(shè)計(jì)(在此感謝 cesium和 Muse為我指出問(wèn)題所在),還真是有點(diǎn)不怎么習(xí)慣哦~
好了,開(kāi)始做個(gè)簡(jiǎn)單的倒計(jì)時(shí)器了。 先讓大家看下運(yùn)行效果吧,顯示在屏幕正中央且置頂顯示:
 
由于比較簡(jiǎn)單,就三個(gè)文件便寫(xiě)完了,分別為界面設(shè)計(jì)的MainWin.xaml和應(yīng)用程序類(lèi)App.xaml 和倒計(jì)時(shí)處理類(lèi)ProcessCount.cs類(lèi)文件。代碼分別如下:
倒計(jì)時(shí)處理類(lèi)ProcessCount.cs :
復(fù)制代碼 代碼如下:

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>
/// 實(shí)現(xiàn)倒計(jì)時(shí)功能的類(lèi)
/// </summary>
public class ProcessCount
{
private Int32 _TotalSecond;
public Int32 TotalSecond
{
get { return _TotalSecond; }
set { _TotalSecond = value; }
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public ProcessCount(Int32 totalSecond)
{
this._TotalSecond = totalSecond;
}
/// <summary>
/// 減秒
/// </summary>
/// <returns></returns>
public bool ProcessCountDown()
{
if (_TotalSecond == 0)
return false;
else
{
_TotalSecond--;
return true;
}
}
/// <summary>
/// 獲取小時(shí)顯示值
/// </summary>
/// <returns></returns>
public string GetHour()
{
return String.Format("{0:D2}", (_TotalSecond / 3600));
}
/// <summary>
/// 獲取分鐘顯示值
/// </summary>
/// <returns></returns>
public string GetMinute()
{
return String.Format("{0:D2}", (_TotalSecond % 3600) / 60);
}
/// <summary>
/// 獲取秒顯示值
/// </summary>
/// <returns></returns>
public string GetSecond()
{
return String.Format("{0:D2}", _TotalSecond % 60);
}
}
}

窗口界面設(shè)計(jì)文件MainWin.xaml
復(fù)制代碼 代碼如下:

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" VerticalAlignment="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" VerticalAlignment="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" VerticalAlignment="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" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="4"/>
</Grid>
</Window>

窗口界面邏輯設(shè)計(jì)文件:MainWin.xaml.cs:
復(fù)制代碼 代碼如下:

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>
/// 窗口加載事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainWin_Loaded(object sender, RoutedEventArgs e)
{
//設(shè)置定時(shí)器
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(10000000); //時(shí)間間隔為一秒
timer.Tick += new EventHandler(timer_Tick);
//轉(zhuǎn)換成秒數(shù)
Int32 hour= Convert.ToInt32(HourArea.Text);
Int32 minute = Convert.ToInt32(MinuteArea.Text);
Int32 second = Convert.ToInt32(SecondArea.Text);
//處理倒計(jì)時(shí)的類(lèi)
processCount = new ProcessCount(hour*3600+minute*60+second);
CountDown += new CountDownHandler(processCount.ProcessCountDown);
//開(kāi)啟定時(shí)器
timer.Start();
}
/// <summary>
/// Timer觸發(fā)的事件
/// </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>
/// 處理事件
/// </summary>
public event CountDownHandler CountDown;
public bool OnCountDown()
{
if (CountDown != null)
return CountDown();
return false;
}
}
/// <summary>
/// 處理倒計(jì)時(shí)的委托
/// </summary>
/// <returns></returns>
public delegate bool CountDownHandler();
}

鑒于代碼中注釋的比較詳細(xì),所以筆者也不再一一贅述了,希望對(duì)大家能有所幫助。完整的工程包下載:http://xiazai.jb51.net/201212/yuanma/CountDown_jb51.rar。

實(shí)例二
效果:
 
UI:放置一個(gè)Label ---><Label Name="lblSecond" FontSize="20" Foreground="Red" ></Label>
CS:
復(fù)制代碼 代碼如下:

  private int countSecond=300; //記錄秒數(shù)
  private void UserControl_Loaded(object sender, RoutedEventArgs e)
  {
    private DispatcherTimer disTimer = new DispatcherTimer();
    disTimer.Interval = new TimeSpan(0, 0, 0, 1); //參數(shù)分別為:天,小時(shí),分,秒。此方法有重載,可根據(jù)實(shí)際情況調(diào)用。
    disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒執(zhí)行的方法
    disTimer.Start();
  }
  void disTimer_Tick(object sender, EventArgs e)
  {
    if(countSecond==0)
    {
      MessageBox.Show("結(jié)束");
    }
    else
    {
      //判斷l(xiāng)blSecond是否處于UI線程上
      if (lblSecond.Dispatcher.CheckAccess())
      {
        lblSecond.Content=countSecnd.ToString();
      }
      else
      {
        lblSecond.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)(() =>{
          lblSecond.Content=countSecond.ToString();
        }));  
      }
      countSecond--;
    }
  }

相關(guān)文章

  • C# Base64編碼

    C# Base64編碼

    Base64編碼的思想是是采用64個(gè)基本的ASCII碼字符對(duì)數(shù)據(jù)進(jìn)行重新編碼。它將需要編碼的數(shù)據(jù)拆分成字節(jié)數(shù)組。
    2009-06-06
  • .Net Core中自定義認(rèn)證實(shí)現(xiàn)

    .Net Core中自定義認(rèn)證實(shí)現(xiàn)

    本文主要介紹了.Net Core中自定義認(rèn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • .NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站

    .NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站

    這篇文章主要為大家介紹了.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • .net使用jwt進(jìn)行身份認(rèn)證的流程記錄

    .net使用jwt進(jìn)行身份認(rèn)證的流程記錄

    這篇文章主要給大家介紹了關(guān)于.net使用jwt進(jìn)行身份認(rèn)證的相關(guān)資料,JWT是Auth0提出的通過(guò)對(duì)JSON進(jìn)行加密簽名來(lái)實(shí)現(xiàn)授權(quán)驗(yàn)證的方案,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)

    asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)

    通過(guò)HTTP模擬GET或POST請(qǐng)求,提交數(shù)據(jù)到服務(wù)端獲取響應(yīng),比較常見(jiàn)些;但如上傳文件到服務(wù)端,使用html form當(dāng)然簡(jiǎn)單了,而因環(huán)境所限有時(shí)需要使用模擬方法去提交有附件(文件上傳)的表單。
    2010-02-02
  • .NET中6種定時(shí)器的用法與特點(diǎn)詳解

    .NET中6種定時(shí)器的用法與特點(diǎn)詳解

    .NET中至少有6種定時(shí)器,每一種定時(shí)器都有它的用途和特點(diǎn),這篇文章主要為大家詳細(xì)介紹了這6種定時(shí)器的基本用法和特點(diǎn),感興趣的小伙伴可以學(xué)習(xí)一下
    2023-11-11
  • 在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟

    在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟

    這篇文章主要介紹了在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 聊聊.Net,Core配置Nlog.md的問(wèn)題

    聊聊.Net,Core配置Nlog.md的問(wèn)題

    這篇文章主要介紹了.Net?Coer配置Nlog.md的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 輕量級(jí)ORM框架Dapper應(yīng)用支持操作函數(shù)和事物

    輕量級(jí)ORM框架Dapper應(yīng)用支持操作函數(shù)和事物

    這篇文章介紹了Dapper支持操作函數(shù)和事物的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • .NET使用System.Timers.Timer類(lèi)實(shí)現(xiàn)程序定時(shí)執(zhí)行

    .NET使用System.Timers.Timer類(lèi)實(shí)現(xiàn)程序定時(shí)執(zhí)行

    這篇文章介紹了.NET使用System.Timers.Timer類(lèi)實(shí)現(xiàn)程序定時(shí)執(zhí)行的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論

恩平市| 岑溪市| 马山县| 当涂县| 咸阳市| 常熟市| 富顺县| 汽车| 沿河| 墨江| 大宁县| 安化县| 织金县| 凤翔县| 民勤县| 陇西县| 紫云| 什邡市| 封丘县| 屏东市| 东乌| 龙川县| 塔河县| 石嘴山市| 彝良县| 石棉县| 清河县| 岳阳市| 哈巴河县| 甘孜县| 泗洪县| 吉木萨尔县| 桂林市| 永昌县| 锡林浩特市| 新宁县| 甘孜县| 桃园市| 繁峙县| 克什克腾旗| 昭通市|