組合框Schedualing Toast通知我編寫一個調度Toast通知,我想的是烤麪包需要的時間從組合框,我想幫助請在UWP
我使用此代碼,開始我的應用程序
(this.DataContext as AlertViewModel).Timer.Start();
這個代碼我把按鈕來啓動我的應用程序,並在組合框中有TimeSpan在AlertViewModel.cs中 在XAML中綁定
我想從Combobox調度Toast通知使用時間,我使用MVVM方法,所以我需要幫助
AlertViewModel類:
public class AlertViewModel : INotifyPropertyChanged
{
public DispatcherTimer Timer { get; set; } = new DispatcherTimer();
public List<TimeSpan> TimeOuts { get; set; } = new List<TimeSpan>();
private TimeSpan timeOut;
public event PropertyChangedEventHandler PropertyChanged;
private int selectedIndex;
public int SelectedIndex
{
get {return selectedIndex;}
set {selectedIndex = value; PropertyChanged(this, new PropertyChangedEventArgs("SelectedIndex")); timeOut = TimeOuts[SelectedIndex];}
}
public List<Sound> Sounds { get; set; } = new List<Sound>();
public event EventHandler<EventArgs> TimeUP;
public AlertViewModel()
{
TimeOuts.Add(new TimeSpan(0, 0, 5));
TimeOuts.Add(new TimeSpan(0, 10, 0)); // 10 Miuntes
TimeOuts.Add(new TimeSpan(0, 15, 0)); // 15 Miuntes
TimeOuts.Add(new TimeSpan(0, 20, 0)); // 20 Miuntes
TimeOuts.Add(new TimeSpan(0, 30, 0)); // 30 Miuntes
TimeOuts.Add(new TimeSpan(1, 0, 0)); // 1 Hour
TimeOuts.Add(new TimeSpan(2, 0, 0)); // 2 Hours
TimeOuts.Add(new TimeSpan(6, 0, 0)); // 6 Hours
TimeOuts.Add(new TimeSpan(12, 0, 0)); // 12 Hours
timeOut = TimeOuts[SelectedIndex];
Timer.Interval = new TimeSpan(0, 0, 1);
Timer.Tick += Timer_Tick;
Sounds.Add(new Sound("Sound 1", "ms-appx:///Assets/Audio/sound1.mp3"));
Sounds.Add(new Sound("Sound 2", "ms-appx:///Assets/Audio/sound2.mp3"));
}
private void Timer_Tick(object sender, object e)
{
timeOut = timeOut.Subtract(new TimeSpan(0, 0, 1));
if (timeOut.TotalSeconds == 0)
{
TimeUP(this, new EventArgs());
timeOut = TimeOuts[SelectedIndex];
}
}
}
和我的XAML代碼:
<ComboBox
x:Name="TimeCombobox"
Margin="50,0"
HorizontalAlignment="Stretch"
ItemsSource="{Binding TimeOuts}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" SelectionChanged="TimeCombobox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Converter={StaticResource TimeSpanToStirngConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
感謝您的回答。但我想這個代碼var deliveryTime = DateTimeOffset.Now.AddSeconds(5);從combobox中選擇他的時間,因爲許多人在combobox中選擇了 –
我用DispatcherTimer做了一個循環,如果我從組合框中使用了5個miuntes,聲音1在每5個miuntes後播放:) –