我编写了一个Scheduling Toast通知,我希望Toast从Combobox中抽出时间,我想要帮助
我使用此代码启动我的应用
(this.DataContext as AlertViewModel).Timer.Start();
我把这个代码放在Button中以启动我的应用程序,并且在combobox中有来自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>
答案 0 :(得分:1)
对于安排Toast通知,我建议使用ScheduledToastNotification Class。这是我们用于安排在特定时间出现的Toast通知的内容。
这个类有两个构造函数。如果您不需要暂停,则可以使用ScheduledToastNotification(XmlDocument, DateTime)构造函数。此构造函数有两个参数,第一个是定义Toast通知内容的XML,第二个是Windows应显示Toast通知的日期和时间。以下是一个简单的示例,显示计划在5秒内显示的Toast通知。
string xml = @"
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Microsoft Company Store</text>
<text>New Halo game is back in stock!</text>
</binding>
</visual>
</toast>";
var content = new Windows.Data.Xml.Dom.XmlDocument();
content.LoadXml(xml);
var deliveryTime = DateTimeOffset.Now.AddSeconds(5);
var toast = new ScheduledToastNotification(content, deliveryTime);
ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
因此,您无需在代码中使用DispatcherTimer
。您可以根据deliveryTime
的所选TimeSpan
计算ComboBox
,然后将其设置为ScheduledToastNotification
。