0

我有一个 MVVM 模式的项目。我也有一个用户控件。此用户控件具有静态 DependencyProperty

public partial class RadarView : INotifyPropertyChanged
{


public static DispatcherTimer Timer { get; set; }

        public static readonly DependencyProperty RequestTypeProperty = DependencyProperty.Register("RequestType", typeof(RadarRequestType), typeof(RadarView), new FrameworkPropertyMetadata(new RadarRequestType(), RequestTypeChanged));

        public RadarRequestType RequestType
        {
            get
            {
                return
                    (RadarRequestType)GetValue(RequestTypeProperty);
            }
            set { SetValue(RequestTypeProperty, value); }
        }

        public static void RequestTypeChanged(DependencyObject dobject, DependencyPropertyChangedEventArgs args)
        {
        var radar = (RadarView)dobject;

            if (Timer == null)
                Timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };

            Timer.Tick += DispatcherTimerTick;

            Timer.Start();

        }

        private static void DispatcherTimerTick(object sender, EventArgs e)
        {
            StartText = DateTime.Now.Second.ToString();

            PropertyChanged(this, new PropertyChangedEventArgs("StartText"));
        }

        public static event PropertyChangedEventHandler PropertyChanged = delegate { };

}

我的问题出在 DispatcherTimerTick 中,因为这与我的用户 Control 相同,而且它不是静态的。此构建错误的消息是:关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效

事实上,我需要将 (RadarView)dobject 发送到 DispatcherTimerTick 事件。我该怎么办?

4

1 回答 1

0

我的问题解决了。我的使用Timer是错误的。我需要radar.Timer

于 2012-05-13T09:40:07.733 回答