本文介绍了模拟时钟ana1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我曾经开发过模拟时钟
这是代码
但这会带来问题
i used to develope an analog clock
here is the code
but it creates problems
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ana1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public interface IClockModel
{
#region ClockDataEventArgs
event EventHandler<ClockDataEventArgs> ClockChanging;
event EventHandler<ClockDataEventArgs> ClockChanged;
void SetClockInterval(TimeSpan timeSpan);
IClockModel Start();
IClockModel Continue();
IClockModel Stop();
ClockData ClockData { get; set; }
bool IsRunning { get; }
}
private void DrawMarkers()
{
MarkerCanvas.Children.Clear();
for (int i = 0; i < 60; ++i)
{
Rectangle markerRectangle = new Rectangle();
if (i % 5 == 0)
{
markerRectangle.Width = 3;
markerRectangle.Height = 8;
markerRectangle.Fill = new SolidColorBrush(Colors.White);
markerRectangle.RenderTransform = this.CreateTransformGroup(i * 6,
-(markerRectangle.Width / 2),
-(markerRectangle.Height * 2 + 4.5 - ClockFaceEllipse.StrokeThickness / 2 - this.clockHeight / 2));
}
else
{
markerRectangle.Width = 1;
markerRectangle.Height = 4;
markerRectangle.Fill = new SolidColorBrush(Colors.White);
markerRectangle.RenderTransform = this.CreateTransformGroup(i * 6,
-(markerRectangle.Width / 2),
-(markerRectangle.Height * 2 + 12.75 - ClockFaceEllipse.StrokeThickness / 2 - this.clockHeight / 2));
}
MarkerCanvas.Children.Add(markerRectangle);
}
}
private TransformGroup CreateTransformGroup(double angle, double firstTranslateXValue, double firstTranslateYValue)
{
TransformGroup transformGroup = new TransformGroup();
TranslateTransform firstTranslate = new TranslateTransform();
firstTranslate.X = firstTranslateXValue;
firstTranslate.Y = firstTranslateYValue;
transformGroup.Children.Add(firstTranslate);
RotateTransform rotateTransform = new RotateTransform();
rotateTransform.Angle = angle;
transformGroup.Children.Add(rotateTransform);
TranslateTransform secondTranslate = new TranslateTransform();
secondTranslate.X = this.clockWidth / 2;
secondTranslate.Y = this.clockHeight / 2;
transformGroup.Children.Add(secondTranslate);
return transformGroup;
}
private void DrawSecondsHand(ClockData clockData)
{
if (secondsHand != null && ClockHandsCanvas.Children.Contains(secondsHand)) ClockHandsCanvas.Children.Remove(secondsHand);
secondsHand = this.CreateHand(this.secondsHandWidth, this.secondsHandHeight, new SolidColorBrush(Colors.Black), 0, 0, 0, null);
secondsHand.RenderTransform = this.CreateTransformGroup(
clockData.Seconds * 6, -this.secondsHandWidth / 2, -this.secondsHandHeight + 4.25);
if (ClockHandsCanvas.Children.Contains(secondsHand) == false)
ClockHandsCanvas.Children.Add(secondsHand);
}
public void Update(ClockData clockData)
{
this.DrawSecondsHand(clockData);
this.DrawMinutesHand(clockData);
this.DrawHoursHand(clockData);
}
public abstract class ClockBase : IClockModel
{
private ClockData clockData;
protected bool isRunning;
protected DispatcherTimer timer;
public event EventHandler<ClockDataEventArgs> ClockChanging;
public event EventHandler<ClockDataEventArgs> ClockChanged;
public ClockBase()
{
clockData = new ClockData();
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
this.SetClockInterval(new TimeSpan(0, 0, 0, 0, 1));
}
public virtual void SetClockInterval(TimeSpan timeSpan)
{
timer.Interval = timeSpan;
}
public ClockData ClockData
{
get
{
return this.clockData;
}
set
{
this.OnClockChangind(new ClockDataEventArgs(this.clockData));
this.clockData = value;
this.OnClockChanged(new ClockDataEventArgs(this.clockData));
}
}
public bool IsRunning
{
get
{
return this.isRunning;
}
}
public abstract IClockModel Start();
public abstract IClockModel Stop();
public abstract IClockModel Continue();
protected abstract void UpdateClockData(DateTime dateTime);
protected virtual void OnClockChangind(ClockDataEventArgs e)
{
EventHandler<ClockDataEventArgs> temp = this.ClockChanging;
if (temp != null)
temp(this, e);
}
protected virtual void OnClockChanged(ClockDataEventArgs e)
{
EventHandler<ClockDataEventArgs> temp = this.ClockChanged;
if (temp != null)
temp(this, e);
}
private void timer_Tick(object sender, EventArgs e)
{
this.OnClockChangind(new ClockDataEventArgs(this.ClockData));
this.UpdateClockData(DateTime.Now);
this.OnClockChanged(new ClockDataEventArgs(this.ClockData));
}
}
public class Clock : ClockBase
{
public static IClockModel Create()
{
return new Clock();
}
public override IClockModel Start()
{
if (IsRunning)
return this;
timer.Start();
isRunning = true;
return this;
}
public override IClockModel Stop()
{
timer.Stop();
isRunning = false;
return this;
}
public override IClockModel Continue()
{
return this.Start();
}
protected override void UpdateClockData(DateTime dateTime)
{
this.ClockData.Update(dateTime);
}
}
public class ClockPresenter
{
public Callback Response;
public ClockPresenter(IClockView clockView, IClockModel clockModel)
{
this.ClockView = clockView;
this.ClockModel = clockModel;
this.ClockView.StartClock += new EventHandler(ClockView_StartClock);
this.ClockView.StopClock += new EventHandler(ClockView_StopClock);
this.ClockModel.ClockChanged += new EventHandler<ClockDataEventArgs>(ClockModel_ClockChanged);
}
public IClockView ClockView
{
get;
set;
}
public IClockModel ClockModel
{
get;
set;
}
private void ClockView_StopClock(object sender, EventArgs e)
{
this.ClockModel.Stop();
}
private void ClockView_StartClock(object sender, EventArgs e)
{
this.ClockModel.Start();
}
private void ClockModel_ClockChanged(object sender, ClockDataEventArgs e)
{
Callback temp = this.Response;
if (temp != null)
temp(e.ClockData);
}
}
//IClockModel clockModel = Clock.Create();
//clockModel.ClockData = new ClockData().Update( DateTime.Now );
//ClockPresenter presenter = new ClockPresenter( this.AnalogClockControl, clockModel );
//presenter.Response += new Callback( this.AnalogClockControl.Update );
//clockModel.Start(); }
}
[snip]
[snip]
推荐答案
cshuhh写道:
但它会产生问题
您能在这里张贴问题(或您得到的错误,以便有人可以帮助您)吗?
Could you post the problems here (or the errors you get so that someone is able to help you)?
这篇关于模拟时钟ana1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!