提醒(警报)从db获取时间和日期,并与当前时间和当前日期匹配 [英] Reminder (alarm) get time and day from db and matching with the current time and current day

查看:86
本文介绍了提醒(警报)从db获取时间和日期,并与当前时间和当前日期匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我必须在每次与当前时间和当前日期(不是日期)匹配时发出警报声的最后一步,时间数据将取自我之前创建的数据库,下面的实体框架是模型我创建了,有两个表,第一个是AlarmModel

  public   int  id { get ;  set ; } 


public int dayId { get ; set ;}
[ForeignKey( dayId)]
public virtual Day day { get ; set ; }

public int sessionId {获得; set ; }
[ForeignKey( sessionId)]
public virtual 会话会话{ get ; set ; }

public DateTime time { get ; set ; }
public int repeatTime { get ; set ; }

public 字符串路径{获得; set ; }
public String keterangan { get ; set ; }



这是第二类DayModel

 公开 
{
public int id { get ; set ; }
public 字符串 nameDay { get ; set ; }
}



下面是我的闹钟表的图像在这里输入图像描述



所以我只关注时间和日期ID示例我将时间设置为08:00,日期是星期日,所以星期日08:00将会有声音响铃路径



我试过这段代码,但我什么都没得到



我尝试过的事情:



  public   class  AlarmClock 
{
public AmanContext repo { get ; set ; }
public AlarmClock()
{
repo = new AmanContext ();
}
public DateTime GetAlarm(DateTime time)
{
DateTime alarm = DateTime.Now;
foreach var item in repo.alarms.ToList())
{
if (time > item.time)
{
alarm = item.time;
}

}

返回闹钟;
}



DispatcherTimer计时器= DispatcherTimer();
public ViewAlarm()
{
this .alarmService = < span class =code-keyword> new AlarmService();
InitializeComponent();

timer.Interval = TimeSpan.FromSeconds( 1 );
timer.Tick + = timer_tick;
timer.Start();



}
AlarmClock ac = new AlarmClock();
private void timer_tick( object sender,EventArgs e)
{
DateTime timeNow = DateTime.Now;

DateTime alarms = ac.GetAlarm(timeNow);
alarmlbl.Content = timeNow.ToLongTimeString();

if (timeNow > 警报)
{
// Sound Something

}
}

解决方案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

Quote:

我已经尝试过这段代码,但我什么都没得到

调试器会告诉你代码在做什么,从而帮助你发现问题所在。


This is the last step which i have to make alarm sound every match with current time and the current day (not date), the data of time will be taken from database which i have created before with entity framework below are models which i created , there are two table , the first is AlarmModel

public int id { get; set;  }


public int dayId {get;set;}
[ForeignKey("dayId")]
public virtual Day day { get; set; }

public int sessionId { get; set; }
[ForeignKey("sessionId")]
public virtual Session session { get; set; }

public DateTime time { get; set; }
public int repeatTime { get; set; }

public String path { get; set; }
public String keterangan { get; set; }


and this is the second class DayModel

public class Day
{
    public int id { get; set; }
    public String nameDay { get; set; }        
}


Below is the image of my alarm table enter image description here

So i just focus to the time and the day id example i set time to 08:00 and day is Sunday so 08:00 on sunday there will be sound ring that is path

I have tried this code , but I get nothing

What I have tried:

public class AlarmClock
    {
        public AmanContext repo { get; set; }
        public AlarmClock()
        {
            repo = new AmanContext();
        }
        public DateTime GetAlarm(DateTime time)
        {
            DateTime alarm = DateTime.Now;
            foreach (var item in repo.alarms.ToList())
            {
                if (time > item.time)
                {
                    alarm = item.time;
                }

            }

            return alarm;
        }



    DispatcherTimer timer = new DispatcherTimer();
        public ViewAlarm()
                {
                    this.alarmService = new AlarmService();
                    InitializeComponent();

                    timer.Interval = TimeSpan.FromSeconds(1);
                    timer.Tick += timer_tick;
                    timer.Start();



                }
                AlarmClock ac = new AlarmClock();
                private void timer_tick(object sender, EventArgs e)
                {
                    DateTime timeNow = DateTime.Now;

                    DateTime alarms = ac.GetAlarm(timeNow);
                    alarmlbl.Content = timeNow.ToLongTimeString();

                    if (timeNow > alarms)
                    {
                        //Sound Something

                    }
                }

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Quote:

I have tried this code , but I get nothing

The debugger will show you what your code is doing and thus help you spot where the problem is.


这篇关于提醒(警报)从db获取时间和日期,并与当前时间和当前日期匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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