在 Windows Phone 8 上创建闹钟时出错? [英] An error when i create an alarms on windows phone 8?

查看:21
本文介绍了在 Windows Phone 8 上创建闹钟时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个闹钟,它计划在今天 6/25/2013(BeginTime) 开始,并且比当前时间晚几个小时.构建时出现错误Microsoft.Phone.ni.dll 中发生类型为‘System.InvalidOperationException’的异常,但未在用户代码中处理".但我更改了 BeginTime 到未来的任何一天,它运行良好.我按照说明 如何为 Windows Phone 创建闹钟和提醒.如何创建带有今天的星星时间的闹钟?

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?

这是我的代码:XAML:

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>

和 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();

    }

推荐答案

我复制了你的代码,没有任何改变,除了这一行之外,它可以完美运行:

I copied your code changed nothing and it would work perfectly except for this line:

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

如果未删除该行(或将文件添加到手机中),则每次添加警报时都会崩溃

which makes it crash EVERYTIME on adding the alarm if that line is not removed (or the file is added to the phone)

只需将其注释掉,您的代码就可以运行了.

Just comment it out and your code should work.

这篇关于在 Windows Phone 8 上创建闹钟时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆