我试图用一个简单的应用程序来解释我的问题:
我有一个只有一个TextBlock的MainWindow。该TextBlock的属性Text与我的CTimer类的属性Seconds绑定。在MainWindow中,我还有一个DispatcherTimer,每秒钟做一件简单的事情 - 增加我的对象的Seconds属性。
MainWindow.xaml:
<Window x:Class="Try.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Name="txtTime" Text="{Binding Seconds}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"/>
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
private CTimer timer = new CTimer();
private DispatcherTimer ticker = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
ticker.Tick += AddSeconds;
ticker.Interval = TimeSpan.FromSeconds(1);
txtTime.DataContext = timer;
ticker.Start();
}
public void AddSeconds(object sender, EventArgs e)
{
timer.Seconds++;
}
}
CTimer.cs:
public class CTimer:INotifyPropertyChanged
{
private int seconds = 0;
public int Seconds
{
get { return seconds; }
set
{
seconds = value;
OnPropertyChanged("Seconds");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
我的问题 - 当我按下三个窗口按钮(最小化/最大化/关闭)中的任何一个并按住不放时,DispatcherTimer 会暂停并保持暂停状态,直到我松开按住的按钮。
有人知道这种行为的原因吗?