0
如何在給定的Timespan中只啓動一次mediaElement。現在它在Button_Click後每秒打開mediaElement。 我知道如何在Button_Click中做到這一點,但我需要Dispatcher,因爲其餘的代碼。 這是Windows應用商店的應用程序。如何在Timespan中只播放一次MediaElement?
感謝
public sealed partial class MainPage : Page
{
DispatcherTimer dt2 = new DispatcherTimer();
public MainPage()
{
this.InitializeComponent();
dt2.Tick += new EventHandler<object>(dt2_Tick);
dt2.Interval = new TimeSpan(0, 0, 0, 1);
mediaElement.Stop();
}
private void dt2_Tick(object sender, object e)
{
var dateNow = DateTime.Now;
var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 12, 0, 0);
TimeSpan start = new TimeSpan(11, 0, 0);
TimeSpan end = new TimeSpan(13, 0, 0);
if(IsTimeOfDayBetween(date, start, end)==true)
{
mediaElement.Start();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dt2.Start();
}
static public bool IsTimeOfDayBetween(DateTime time,
TimeSpan start, TimeSpan end)
{
if (end == start)
{
return true;
}
else if (end < start)
{
return time.TimeOfDay <= end ||
time.TimeOfDay >= start;
}
else
{
return time.TimeOfDay >= start &&
time.TimeOfDay <= end;
}
}
}