wpf時鐘程式(總結)

來源:互聯網
上載者:User

   學C#沒多久,培訓了一個星期剛講到WPF,就來任務了。需要寫一個WPF程式,要用資料繫結和線程來寫一個轉動的時鐘。

想想就鬱悶,C#基礎還沒學好,XAML也不熟,對前台和後台線程還沒有一個清楚的概念。就在這樣的情況下開始寫了自己的第一個WPF程式。。

    構思了一下:UI就做簡單點好了,畫個圓當錶盤,然後用三個Rectangle當做指標吧,轉動就用RotateTransform類來做,把角度和後面的類的屬性綁定就行了綁定寫法MSDN上有,注意是TwoWay的MODE就是了。。

    前面是搞定了剩下的就是後面邏輯表示的代碼了。寫了一個綁定用到的類,實現了INotifyPropertyChanged介面的PropertyChanged方法,這樣當這個類的屬性改變後就會自己動通知綁定的UI屬性,當然繫結來源必需是DependencyProperty類型的。

    然後,用了一個Timer設定一秒的時間間隔,我把角度的變化和類屬性改變的函數都放在了Timer的Elapsed事件裡用線程來處理,

後台線程要更新UI直接寫是不行的,可以用Dispatcher,Dispatcher  類提供兩個註冊工作項目的方法:Invoke 和 BeginInvoke;這兩個方法均調度一個委託來執行。Invoke 是同步調用, 也就是說,直到 UI 線程實際執行完該委託它才返回。BeginInvoke 是非同步,將立即返回。這樣在我設定的時間間隔內類的屬性在跟系統時間變化,這樣UI的屬性Rectangle的角度,也會被通知更新,綁定就完成了!

後面想想為了美觀,個錶盤用了個ICO可以自己找找代替吧

 

代碼如下:XAML

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:WpfApplication2"
    Title="Window1" Margin="2" Height="327" Width="311" AllowsTransparency="True"
        WindowStyle="None" Background="Transparent" WindowStartupLocation="CenterScreen"
        Icon="clock.ico"
        ResizeMode="NoResize" Topmost="False" Opacity="1">
 
    <Grid  x:Name="grid" Width="300" Height="300">
      
       
        <Image Source="images/backGround.png"></Image>
       
            <!-- Second  -->
        <Rectangle  Margin="150,0,149,150" Name="rectangleSecond" Stroke="White" Height="120" VerticalAlignment="Bottom">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="secondHand" CenterX="0" CenterY="120" Angle="{Binding  Path=Second,Mode=TwoWay}" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!-- -->

        <!-- Minute  -->
        <Rectangle Margin="150,49,149,151" Name="rectangleMinute" Stroke="LightGreen">
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="minuteHand" CenterX="0" CenterY="100" Angle="{Binding Path=Minute,Mode=TwoWay}" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!-- -->
        <!-- Hour  -->
        <Rectangle Margin="150,80,149,150" Name="rectangleHour" Stroke="LightYellow">
           
            <Rectangle.RenderTransform>
                <RotateTransform x:Name="hourHand" CenterX="0" CenterY="70" Angle="{Binding  Path=Hour,Mode=TwoWay}"  />
            </Rectangle.RenderTransform>
        </Rectangle>
        <!---->
    </Grid>

</Window>

。。。。。。。。。。。。。。。。。。。

。。。。。。。。。。。。。。。。。。。

 

後台代碼:.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        System.Timers.Timer timer = new System.Timers.Timer(1000);
        public Window1()
        {
            InitializeComponent();
            DateTime date = DateTime.Now;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
        }

        /// <summary>
        /// 計算時鐘轉動角度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
           
            Clock cl = new Clock();
            //後台線程可以請求 UI 線程代表它執行操作。
            //這是通過向 UI 線程的 Dispatcher 註冊工作項目來完成的。
            //Dispatcher 類提供兩個註冊工作項目的方法:Invoke 和 BeginInvoke。
            //這兩個方法均調度一個委託來執行。Invoke 是同步調用,
            //也就是說,直到 UI 線程實際執行完該委託它才返回。
            //BeginInvoke 是非同步,將立即返回。
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
            {
                cl.Second = DateTime.Now.Second * 6;
                cl.Minute= DateTime.Now.Minute * 6;
                cl.Hour= (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);
                grid.DataContext = cl;
               

            }));
        }
    }
    /// <summary>
    /// 資料繫結類
    /// </summary>
    public class Clock :INotifyPropertyChanged
    { 
        #region NotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
           
            if (PropertyChanged != null)
            { //實現INotifyPropertyChanged中的PropertyChanged方法
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }

        }
        #endregion
        #region Property Members
        private double hour = 0;
        public double Hour
        {
            get { return hour; }
            set
            {
                if (value != hour)
                {
                    hour = value;
                    NotifyPropertyChanged("Hour");
                }
            }
        }

        private double minute = 0;
        public double Minute
        {
            get { return minute; }
            set
            {
                if (value != minute)
                {
                    minute = value;
                    NotifyPropertyChanged("Minute");
                }
            }
        }

        private double second = 0;
        public double Second
        {
            get { return second; }
            set
            {
                if (value != second)
                {
                    second = value;
                    NotifyPropertyChanged("Second");
                }
            }
        }
        #endregion

    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.