如何将以秒为单位的时间转换为每秒更新的角度? [英] How do I convert time in seconds to angle that updates every second?

查看:119
本文介绍了如何将以秒为单位的时间转换为每秒更新的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace WpfApplication3
{
    public class UpdatableDateTime : INotifyPropertyChanged
    {
        public DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        private DispatcherTimer tmrDoUpdate;
        
        public UpdatableDateTime()
        {
            tmrDoUpdate = new DispatcherTimer();
            tmrDoUpdate.Interval = TimeSpan.FromSeconds(1);
            tmrDoUpdate.Tick += new EventHandler(tmrDoUpdate_Tick);
            tmrDoUpdate.Start();
        }

        void tmrDoUpdate_Tick(object sender, EventArgs e)
        {
            OnPropertyChanged(new PropertyChangedEventArgs("Now"));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        #endregion
  }
}


这将是我在wpf中创建的时钟.
我已经有几秒钟了,但是我仍然需要将其转换为角度.

当我包含"* 6"时,下面的语法不起作用,这是秒到角度的转换,因此我必须在类内进行转换


This will be for a clock i am creating in wpf.
I already got seconds but i still need to convert it in to angle.

The syntax below dont work when I include "*6" which is the conversion of seconds to angle so I have to convert it inside the class

<rotatetransform angle="{Binding Source={StaticResource updatableDateTime*6}, Path=ConvertMinute}"></rotatetransform>

推荐答案

首先,您上面的类看起来正确,如果您正在考虑此问题,则无需添加角度.

其次,创建一个转换器类(从IValueConverter继承),该类花费时间并从给定的秒数(秒数* 6)中返回角度.在XAML中包含转换器的名称空间,然后将转换器传递到绑定中:
Firstly, the class you have above looks correct, there is no need to add an angle to it in case you were thinking of this.

Secondly, Create a converter class (inheriting from IValueConverter) that takes the time and returns the angle from the given number of seconds (Number of Seconds * 6). Include the converter''s namespace in the XAML, then pass the converter into the binding:
{Binding {StaticResource {StaticResource updatableDateTime}, Converter={StaticResource YourConverterName}}



这是有关转换器的文章:
WPF中的值转换器 [



Here is an article about converters:
Value Converter in WPF[^]


一种方法是使用像Keith Barrow这样的转换器建议.另一种方法是更改​​数据模型-添加一个名为Angle的属性,在代码中进行所有必要的计算并绑定到新属性.

第二种方法实现起来更简单,更快捷,但是取决于您选择哪种解决方案来满足您的需求...
One way to do it is to use a converter like Keith Barrow suggested. Another way would be to change your data model - add a property called Angle, do all necessary calculations in code and bind to the new property.

The second method is simpler and faster to implement, but it''s up to you to choose which solution fits your needs...


这篇关于如何将以秒为单位的时间转换为每秒更新的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆