如何使用Windows Phone中的定期代理

时间:2014-06-23 10:34:32

标签: c# xml xaml windows-phone-7 windows-phone-8

我在我的应用程序中使用周期性代理我必须在用户设置的给定间隔内以30分钟的频率显示引号。在下面的调用方法中,我正在读取xml文件以获取start的值时间,结束时间和频率,并在另一个尝试抓住获取引号的xml文件。 我想在30分钟后调用invoke方法来显示行情..

protected override void OnInvoke(ScheduledTask task)
    {
        TimeSpan minute;
        TimeSpan hour;

        try
        {

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Reminder.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<ReminderModel>));
                    List<ReminderModel> data = (List<ReminderModel>)serializer.Deserialize(stream);

                    foreach (var item in data)
                    {
                        //if (item.StartTime <=item.EndTime)
                        if (item.StartTime < DateTime.Now && item.EndTime >= DateTime.Now)
                        {
                            if (item.Frequency.TimeOfDay.Hours == 0)
                            {
                                minute = new TimeSpan(item.Frequency.TimeOfDay.);
                                // minute = new TimeSpan(item.Frequency.Minute);

                                item.StartTime.AddMinutes(item.Frequency.TimeOfDay.Minutes);
                            }
                            else
                            {
                                 hour = new TimeSpan(item.Frequency.Hour);
                                item.StartTime.AddHours(item.Frequency.TimeOfDay.Hours);
                            }

                        }
                        else
                        {
                            MessageBox.Show("The Periodic agent is stop");
                        }



                    }

                }
            }

        }

        catch (Exception)
        {
        }


        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Notification.xml", FileMode.Open))
                {

                    using (StreamReader reader = new StreamReader(stream))
                    {
                        var Data = reader.ReadToEnd();
                        var value = Data.Replace("\r\n", "").Remove(0, 74).Split('<');
                        this.QuoteRemoveList.Clear();
                        foreach (var item in value)
                        {
                            if (item.StartsWith("QuoteElement"))
                            {

                                this.QuoteRemoveList.Add(item.Remove(0, 13));
                            }


                        }
                    }

                }


            }
        }

        catch
        { }


        {

            Random rd = new Random();


            string toastMessage = QuoteRemoveList[rd.Next(0, 99)];


            ShellToast toast = new ShellToast();
            toast.Title = "Quotes";
            toast.Content = toastMessage;
            toast.Show();
            NotifyComplete();

            // If debugging is enabled, launch the agent again in one minute.
            //#if DEBUG_AGENT
            // ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
            //#endif


                if (this.Agent.hour>0)
                {
                    ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromHours(personalViewModel.Frequency.TimeOfDay.Hours));
                }
                else if (this.Agent.minute > 0)
                {
                    ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromMinutes(personalViewModel.Frequency.Minute));
                }
            }




        }

    }

1 个答案:

答案 0 :(得分:0)

从这里(Background agents for Windows Phone 8)你可以详细说明Background agents如何在Windows手机中工作,从这里(How to implement background agents for Windows Phone 8)可以获得指南 如何为Windows Phone 8实现后台代理。在imeplementing之前请参阅第一个链接中的指南和说明。