我有一个带倒计时功能的控制窗口。需要倒计时值以显示在不同的窗口上。为此,我创建了一个类/视图模型如下:
public class RunningTime : INotifyPropertyChanged
{
public string runtime = "";
public RunningTime()
{
}
public string RunTime
{
get
{
return runtime;
}
set
{
if (runtime != value)
{
runtime = value;
OnPropertyChanged("RunTime");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
之后,在控制窗口中,我有一个文本块和一个按钮。 Textblock显示倒计时时间,按钮开始计数。
<TextBox Name="txtTime" IsReadOnly="True"
Text="{Binding RunTime}"></TextBox>
<Button Name="btnStart" Content="START"
Click="btnStart_Click"></Button>
在代码背后,我有以下内容:
namespace Stadium
{
public partial class Window_Control : Window
{
RunningTime _runtime = new RunningTime();
private DispatcherTimer Timer;
private int runtime = 65;
public static string str_time = "";
public Window_Control()
{
InitializeComponent();
base.DataContext = _runtime;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Timer = new DispatcherTimer();
Timer.Interval = new TimeSpan(0, 0, 1);
Timer.Tick += Timer_Tick;
Timer.Start();
}
public void Timer_Tick(object sender, EventArgs e)
{
if (runtime > 1)
{
runtime--;
string min = ((runtime / 60) < 10) ? "0" + (runtime / 60).ToString() : (runtime / 60).ToString();
string sec = ((runtime % 60) < 10) ? "0" + (runtime % 60).ToString() : (runtime % 60).ToString();
str_time = string.Format("{0}:{1}", min, sec);
_runtime.RunTime = str_time;
}
else
{
Timer.Stop();
MessageBox.Show("Time's Up");
}
}
在第二个窗口中,应该在控制窗口中显示倒计时,我也有一个文本块。
<TextBlock Name="txtGameTime"
Text="{Binding Source={StaticResource myDataSource}, Path=RunTime}"></TextBlock>
太糟糕了我无法在第二个窗口显示时间。 如何显示倒计时时需要帮助。
非常感谢。
UPDATE(第二个窗口XAML):
<Window x:Class="Stadium.Window_Game"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stadium">
<Window.Resources>
<local:RunningTime x:Key="myDataSource"/>
</Window.Resources>
<TextBlock Name="txtGameTime"
Text="{Binding Source={StaticResource myDataSource}, Path=RunTime}"></TextBlock>
答案 0 :(得分:1)
将您的计时器逻辑添加到RunningTime类,如下所示:
public class RunningTime: INotifyPropertyChanged
{
public string runtime = "";
private DispatcherTimer timer;
private int runtimeInt = 65;
public static string str_time = "";
public RunningTime()
{
LoadTimer();
}
public string RunTime
{
get
{
return runtime;
}
set
{
if (runtime != value)
{
runtime = value;
OnPropertyChanged("RunTime");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
public void LoadTimer()
{
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += Timer_Tick;
timer.Start();
}
public void Timer_Tick(object sender, EventArgs e)
{
if (runtimeInt > 1)
{
runtimeInt--;
string min = ((runtimeInt / 60) < 10) ? "0" + (runtimeInt / 60).ToString() : (runtimeInt / 60).ToString();
string sec = ((runtimeInt % 60) < 10) ? "0" + (runtimeInt % 60).ToString() : (runtimeInt % 60).ToString();
str_time = string.Format("{0}:{1}", min, sec);
this.RunTime = str_time;
}
else
{
timer.Stop();
MessageBox.Show("Time's Up");
}
}
}
在Window_Control类中,您只需要以下内容:
public partial class Window_Control : Window
{
RunningTime _runtime = new RunningTime();
public Window_Control()
{
InitializeComponent();
base.DataContext = _runtime;
}
}
第二个窗口的xaml将是:
<Window x:Class="Stadium.Window_Game"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Stadium">
<Window.Resources>
<local:RunningTime x:Key="myDataSource"/>
</Window.Resources>
<TextBlock Name="txtGameTime"
Text="{Binding Source={StaticResource myDataSource}, Path=RunTime}"></TextBlock>