如何使用MVVM创建定时弹出窗口?

发布于 2024-12-04 22:33:42 字数 1394 浏览 0 评论 0原文

我正在创建一个基于 MVVM Light 的警报应用程序。

该应用程序的主要功能是在特定时间弹出警报消息。 我创建了一个视图 Alarm.xaml,在其中创建并保存带有警报的任务、模型类 Task.cs 和视图模型类 AlarmViewModel.cs< /代码>。

接下来,我创建了一个计时器,每半分钟根据任务列表检查当前时间:

System.Timers.Timer timer;
//I am using Timer class on purpose because I want to have asynchronous behavior 

private void InitTimer()
{
    timer = new Timer(30000); //Check every 30 seconds
    timer.Enabled = true;
    timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
    timer.Start();
}

private void TimerElapsed(object sender, ElapsedEventArgs e)
{
    DateTime currentTime;
    string message;

    currentTime = e.SignalTime;
    foreach (Task task in tasks)
    {
        if (task.AlarmTime.CompareTo(currentTime) <= 0)
        {
            message = string.Format("({0}) Task:\n{1}", 
                task.AlarmTime.ToString("dd/MMM/yy HH:mm"), task.Description);
            //This message needs to pop up
        }
    }
}

我有两个问题:

  1. 第一个问题是初始化和启动计时器的最佳位置是什么?现在,上面编写的两种方法位于 AlarmViewModel.cs 类中,但我打算在我的应用程序中拥有多个窗口,因此有更多的视图模型,并且我希望进行警报检查无论 Alarm.xaml 窗口是否打开。我需要一种中心位置来让计时器在应用程序运行时保持运行。
  2. 第二个问题是如何让 TimerElapsed 事件处理程序中的字符串消息弹出?我想创建一个单独的窗口/控件(和相应的视图模型)来显示任务描述。但是,如果我从视图模型层控制所有内容,如何使该窗口/控件出现(即弹出)?如何编排窗口? Viewmodel 定位器(MVVM 内的组件)?如何?

感谢您的所有帮助。干杯。

I am creating an Alarm application based on MVVM Light.

The main functionality of the app is to popup an alarm messages at specific times.
I have created a view Alarm.xaml where I create and save the tasks with alarms, a model class Task.cs, and a viewmodel class AlarmViewModel.cs.

Next, I have created a timer, that checks the current time against the list of tasks every half minute:

System.Timers.Timer timer;
//I am using Timer class on purpose because I want to have asynchronous behavior 

private void InitTimer()
{
    timer = new Timer(30000); //Check every 30 seconds
    timer.Enabled = true;
    timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
    timer.Start();
}

private void TimerElapsed(object sender, ElapsedEventArgs e)
{
    DateTime currentTime;
    string message;

    currentTime = e.SignalTime;
    foreach (Task task in tasks)
    {
        if (task.AlarmTime.CompareTo(currentTime) <= 0)
        {
            message = string.Format("({0}) Task:\n{1}", 
                task.AlarmTime.ToString("dd/MMM/yy HH:mm"), task.Description);
            //This message needs to pop up
        }
    }
}

I have two questions:

  1. The first is what is the best location to initialize and start the timer? Right now, the two methods written above are located in the AlarmViewModel.cs class, but I intend to have more than one window in my app, and therefore more viewmodels, and I want my alarm checks to occur regardless of whether the Alarm.xaml window is open or not. I need a kind of a central place to keep the timer running for as long as the application is running.
  2. The second question is how to make the string message from TimerElapsed event handler pop up? I want to create a separate window/control (and a corresponding viewmodel) to show the task description. But how do I make that window/control appear (i.e. pop up) if I am controlling everything from the viewmodel tier? How is orchestrating the windows? Viewmodel locator (a component inside MVVM)? How?

Thanks for all the help. Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-12-11 22:33:42

您可以使用 PRISM 轻松完成此操作(以及更多操作):http://compositewpf.codeplex.com/

1:创建一个可以由不同视图模型加载的模块作为提供警报触发器的中央服务。在编写应用程序时,加载该模块并将其与视图模型绑定。

对于 2:PRISM 支持所谓的交互请求,允许您以 MVVM 纯粹方式从视图模型弹出对话框(不违反视图对视图模型的单向依赖性)。它的工作原理类似于发送到 UI 的事件。请阅读 PRISM 指南(也可在上面的链接中找到)以查找实现此目的的具体代码示例。

希望有帮助。

You can do this (and much more) with great ease using PRISM: http://compositewpf.codeplex.com/.

For 1: Create a module that can be loaded by different viewmodels as a central service that offers the alarm triggers.While composing your application, load the module and bind it with the viewmodels.

For 2: PRISM supports the so-called Interaction Requests that allow you to pop-up dialogs in from the viewmodel in a MVVM pure way (without violating the single direction dependency of the view on the viewmodel). It works like an event send to the UI. Please read the PRISM guide (also available on above link) to find concrete code examples to achieve this.

Hope that helps.

幽梦紫曦~ 2024-12-11 22:33:42

对于 1:我可能会将计时器放在应用程序中或视图定位器中。如果您使用 IoC 容器(例如 Unity),将其放在那里可能是一个好主意。

对于 2:您可以查看这篇文章了解处理对话框的策略在MVVM中。

For 1: I'd probably put the timer either ito the application or into the view locator.If you using a IoC Container (Unity for example) putting it there might be a good idea.

For 2: you can see this post for strategies handling dialogs in MVVM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文