2

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.

Boris
  • 9,986
  • 34
  • 110
  • 147

2 Answers2

1

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.

kroonwijk
  • 8,340
  • 3
  • 31
  • 52
  • Thanks for the reply. I will look into PRISM, although I am quite new with MVVM Light anyway and I am a bit worried that I will get far too confused if I start multi-threading now :) I was wondering if you could perhaps elaborate a bit to get me started with your solution for the first question. What do you mean by module and how to bind it to the viewmodels? Thanks! – Boris Sep 13 '11 at 19:49
  • PRISM does the same as MVVM light, plus more. It is about being able to assemble a product at runtime, by composing it using so-called modules. A lot of design patterns and techniques are incorporated in PRISM that made me choose it over other MVVM libraries. Do me a favor and please scroll through the examples in the PDF downloadable at http://compositewpf.codeplex.com/releases/view/55580. I cannot give you a better starting point myself :-) – kroonwijk Sep 13 '11 at 20:01
  • Got it! `Modules` are a term defined in PRISM paradigm. It's time to roll up the sleeves – Boris Sep 13 '11 at 20:06
  • Once you get the hang of PRISM, you will never be the same again! – kroonwijk Sep 13 '11 at 20:11
0

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.

Community
  • 1
  • 1
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70