2013-06-25 45 views
-4

在我的应用程序,我有一个报警,计划今天开始2013年6月25日(BeginTime)和比当前时间晚数小时。当我建立我有一个错误“在Microsoft.Phone.ni.dll中发生类型'System.InvalidOperationException'的异常,但没有在用户代码”处理“。但我改变BeginTime到任何一天在未来,它的工作原理好,我按照说明How to create alarms and reminders for Windows Phone。我如何在今天用星空创建闹钟?当我在windows phone 8上创建闹钟时出现错误?

这是我的代码: XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <ScrollViewer> 
      <StackPanel Orientation="Vertical"> 
       <StackPanel Orientation="Horizontal"> 
        <RadioButton Content="Reminder" Name="reminderRadioButton" GroupName="ReminderOrAlarm" IsChecked="True"></RadioButton> 
        <RadioButton Content="Alarm" Name="alarmRadioButton" GroupName="ReminderOrAlarm" ></RadioButton> 
       </StackPanel> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="titleLabel" Text="title" VerticalAlignment="Top" /> 
       <TextBox Height="72" HorizontalAlignment="Left" Name="titleTextBox" Text="" VerticalAlignment="Top" Width="460" MaxLength="63"/> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="contentLabel" Text="content" VerticalAlignment="Top" /> 
       <TextBox Height="160" HorizontalAlignment="Left" Name="contentTextBox" Text="" VerticalAlignment="Top" Width="460" TextWrapping="Wrap" MaxLength="256" AcceptsReturn="True" /> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="beginTimeLabel" Text="begin time" VerticalAlignment="Top" /> 
       <StackPanel Orientation="Horizontal"> 
        <toolkit:DatePicker x:Name="beginDatePicker" Width="220" HorizontalAlignment="Left"></toolkit:DatePicker> 
        <toolkit:TimePicker x:Name="beginTimePicker" Width="220" HorizontalAlignment="Right"></toolkit:TimePicker> 
       </StackPanel> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="expirationTimeLabel" Text="expiration time" VerticalAlignment="Top" /> 
       <StackPanel Orientation="Horizontal"> 
        <toolkit:DatePicker x:Name="expirationDatePicker" Width="220" HorizontalAlignment="Left"></toolkit:DatePicker> 
        <toolkit:TimePicker x:Name="expirationTimePicker" Width="220" HorizontalAlignment="Right"></toolkit:TimePicker> 
       </StackPanel> 
       <Grid> 
        <RadioButton Content="once" Height="72" HorizontalAlignment="Left" Margin="0,0,0,0" Name="onceRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval" IsChecked="True"/> 
        <RadioButton Content="weekly" Height="72" HorizontalAlignment="Left" Margin="0,70,0,0" Name="weeklyRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval"/> 
        <RadioButton Content="daily" Height="72" HorizontalAlignment="Left" Margin="0,140,0,0" Name="dailyRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval"/> 
        <RadioButton Content="monthly" Height="72" HorizontalAlignment="Left" Margin="240,0,0,0" Name="monthlyRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval"/> 
        <RadioButton Content="end of month" Height="72" HorizontalAlignment="Left" Margin="240,70,0,0" Name="endOfMonthRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval"/> 
        <RadioButton Content="yearly" Height="72" HorizontalAlignment="Left" Margin="240,140,0,0" Name="yearlyRadioButton" VerticalAlignment="Top" GroupName="ScheduleInterval"/> 
       </Grid> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="param1Label" Text="context parameter 1" VerticalAlignment="Top" /> 
       <TextBox Height="72" HorizontalAlignment="Left" Name="param1TextBox" Text="" VerticalAlignment="Top" Width="440" MaxLength="63"/> 
       <TextBlock Height="30" HorizontalAlignment="Left" Name="param2Label" Text="context parameter 2" VerticalAlignment="Top" /> 
       <TextBox Height="72" HorizontalAlignment="Left" Name="param2TextBox" Text="" VerticalAlignment="Top" Width="440" MaxLength="63"/> 

      </StackPanel> 
     </ScrollViewer> 
    </Grid> 

和C#:

private void ApplicationBarSaveButton_Click(object sender, EventArgs e) 
    { 
     String name = System.Guid.NewGuid().ToString(); 
     DateTime date = (DateTime)beginDatePicker.Value; 
     DateTime time = (DateTime)beginTimePicker.Value; 
     DateTime beginTime = date + time.TimeOfDay; 
     if (beginTime < DateTime.Now) 
     { 
      MessageBox.Show("the begin date must be in the future."); 
      return; 
     } 
     date = (DateTime)expirationDatePicker.Value; 
     time = (DateTime)expirationTimePicker.Value; 
     DateTime expirationTime = date + time.TimeOfDay; 

     if (expirationTime < beginTime) 
     { 
      MessageBox.Show("expiration time must be after the begin time."); 
      return; 
     } 
     RecurrenceInterval recurrence = RecurrenceInterval.None; 
     if (dailyRadioButton.IsChecked == true) 
     { 
      recurrence = RecurrenceInterval.Daily; 
     } 
     else if (weeklyRadioButton.IsChecked == true) 
     { 
      recurrence = RecurrenceInterval.Weekly; 
     } 
     else if (monthlyRadioButton.IsChecked == true) 
     { 
      recurrence = RecurrenceInterval.Monthly; 
     } 
     else if (endOfMonthRadioButton.IsChecked == true) 
     { 
      recurrence = RecurrenceInterval.EndOfMonth; 
     } 
     else if (yearlyRadioButton.IsChecked == true) 
     { 
      recurrence = RecurrenceInterval.Yearly; 
     } 

     string param1Value = param1TextBox.Text; 
     string param2Value = param2TextBox.Text; 
     string queryString = ""; 
     if (param1Value != "" && param2Value != "") 
     { 
      queryString = "?param1=" + param1Value + "&param2=" + param2Value; 
     } 
     else if (param1Value != "" || param2Value != "") 
     { 
      queryString = (param1Value != null) ? "?param1=" + param1Value : "?param2=" + param2Value; 
     } 
     Uri navigationUri = new Uri("/ShowParams.xaml" + queryString, UriKind.Relative); 
     if ((bool)reminderRadioButton.IsChecked) 
     { 
      Reminder reminder = new Reminder(name); 
      reminder.Title = titleTextBox.Text; 
      reminder.Content = contentTextBox.Text; 
      reminder.BeginTime = beginTime; 
      reminder.ExpirationTime = expirationTime; 
      reminder.RecurrenceType = recurrence; 
      reminder.NavigationUri = navigationUri; 

      // Register the reminder with the system. 
      ScheduledActionService.Add(reminder); 
     } 
     else 
     { 
      Alarm alarm = new Alarm(name); 
      alarm.Content = contentTextBox.Text; 
      alarm.Sound = new Uri("/Ringtones/Ring01.wma", UriKind.Relative); 
      alarm.BeginTime = beginTime; 
      alarm.ExpirationTime = expirationTime; 
      alarm.RecurrenceType = recurrence; 

      ScheduledActionService.Add(alarm); 
     } 
     // Navigate back to the main reminder list page. 
     NavigationService.GoBack(); 

    } 
+0

您需要发布您正在使用的代码,更重要的是,该例外的消息。你没有提供足够的信息,而且很可能你根本没有设置闹钟的时间。 –

回答

0

我复制你的代码,什么都没有改变,它会除了这行工作完美:

alarm.Sound = new Uri("/Ringtones/Ring01.wma", UriKind.Relative);

这使它崩溃EVERYTIME如果该线路没有被移除(或者该文件被添加到电话),则添加该警报

只需对其进行注释并且您的代码应该可以工作。

+0

哦,谢谢,我错了文件名。 – thefriend