UWP Implementation Clock

Source: Internet
Author: User

The UWP is really easy to develop now, but the data is so little ...

A few days ago to see the students have a real realization of the custom clock, this stuff is quite simple, I also write a, no Chengxiang, met a pit, took a long time, write down a bit.

Draw a circle, three lines just fine. The XAML code is as follows:

 <GridBackground="{ThemeResource Applicationpagebackgroundthemebrush}">            <Ellipsex:name= "Ellipse"Stroke= "Black"/>            < Linex:name= "Line_second"Stroke= "Red"strokethickness= "3"/>            < Linex:name= "Line_minute"Stroke= "Blue"strokethickness= "2"/>            < Linex:name= "Line_hour"Stroke= "Black"strokethickness= "1"/></Grid>

C # code:

  Public Sealed Partial classmainpage:page {dispatchertimer timer;//Defining Timers        DoubleScreenHeight; DoubleScreenWidth; DoubleEllipsewidth; Const DoublePI =3.1415926; Const DoubleLinelength_second= -; Const DoubleLinelenght_minute = $; Const DoubleLinelenght_hour = Max; Static intsecond; Static intminute; Static inthour;  PublicMainPage () { This.            InitializeComponent (); Timer=NewDispatcherTimer (); Timer. Interval=NewTimeSpan (0,0,1); Timer. Tick+ = Timer_tick;//This event is triggered every second to refresh the pointertimer.        Start (); }        Private voidTimer_tick (ObjectSenderObjecte) {line_second.x1= Window.Current.Bounds.Width/2; Line_second.y1= Window.Current.Bounds.Height/2; Line_minute.x1= Window.Current.Bounds.Width/2; Line_minute.y1=window.current.bounds.height/2; Line_hour.x1= Window.Current.Bounds.Width/2; Line_hour.y1= Window.Current.Bounds.Height/2; //secondsSecond =DateTime.Now.Second; DoubleRadius_second = PI * Second/ -; Line_second.x2= Linelength_second * Math.sin (Radius_second) +line_second.x1; Line_second.y2= Line_second.y1-linelength_second *Math.Cos (Radius_second); //minuteminute =DateTime.Now.Minute; Doubleradius_minute= PI * Minute/ -; Line_minute.x2= Linelenght_minute * Math.sin (Radius_minute) +line_minute.x1; Line_minute.y2= Line_minute.y1-linelenght_minute *Math.Cos (Radius_minute); //Hourhour =DateTime.Now.Hour; DoubleRadius_hour = hour*pi/6+radius_minute/ A; Line_hour.x2= Linelenght_hour * Math.sin (Radius_hour) +line_hour.x1; Line_hour.y2= Line_hour.y1-linelenght_hour *Math.Cos (Radius_hour); }        protected Override voidonnavigatedto (NavigationEventArgs e) {Base.            Onnavigatedto (e); //to get the actual length and width of the window, the following is to apply a different form//the length of a circle or even a straight line varies with the formScreenHeight =Window.Current.Bounds.Height; ScreenWidth=Window.Current.Bounds.Width; Ellipsewidth= screenwidth > ScreenHeight? ScreenHeight- -: ScreenWidth- -; Ellipse. Height=Ellipsewidth; Ellipse. Width=Ellipsewidth; }    }

The foundation was not very good, and the result was a long time before the pointer was right, because I was not clear about the 0° position of the UWP angle:

The upper left corner of the form is the origin of the coordinates, and the angle of the 0° position, adjusted for a long time, I thought the writing is wrong, the original coordinate system was wrong, hey

UWP Implementation Clock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.