In my app, i have an alarm which is scheduled to start on today 6/25/2013(BeginTime)and hours later than the current time. When building i have an error "An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.ni.dll but was not handled in user code".But i change BeginTime to any day in future, it works well.I follow the instructions How to create alarms and reminders for Windows Phone. How can i create an alarm with star time on today?
This is my code:
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>
And 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 + "¶m2=" + 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();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…