我编写了计划吐司通知,我希望吐司从Combobox中抽出时间,我需要帮助
我使用此代码启动我的应用
(this.DataContext as AlertViewModel).Timer.Start();
我将这些代码放入Button中以启动我的应用,并且在组合框中的时间是AlertViewModel.cs中TimeSpan的时间
在XAML中绑定(bind)
我想从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>
最佳答案
为了安排敬酒通知,我建议使用ScheduledToastNotification Class。这就是我们用来计划在特定时间出现的敬酒通知的方式。
此类具有两个构造函数。如果您不需要贪睡,可以使用ScheduledToastNotification(XmlDocument, DateTime)构造函数。该构造函数有两个参数,第一个是定义Toast通知内容的XML,第二个是Windows显示Toast通知的日期和时间。下面是一个简单的示例,显示了计划在5秒钟内显示的敬酒通知。
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
。关于c# - 在UWP中使用组合框计划 toast 通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42704014/