在UWP

2017-03-09 48 views
1

組合框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> 

回答

1

調度Toast通知,我建議使用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。您可以根據您的ComboBox的所選TimeSpan計算deliveryTime,然後將其設置爲ScheduledToastNotification

+0

感謝您的回答。但我想這個代碼var deliveryTime = DateTimeOffset.Now.AddSeconds(5);從combobox中選擇他的時間,因爲許多人在combobox中選擇了 –

+0

我用DispatcherTimer做了一個循環,如果我從組合框中使用了5個miuntes,聲音1在每5個miuntes後播放:) –