Exercise 3: Window Handling

In the previous section, our notification popup told the user that there are attendees needing attention, but didn’t offer an easy way to get back to the application. Users could reasonably expect that clicking on the window would take them to the main window. So we’ll make that happen using Silverlight 4’s new window management features.

Managing the Windows

  1. In the NotificationContent.xaml.cs or NotificationContent.xaml.vb code behind’s constructor, add a handler for the MouseLeftButtonDown event. In this handler, add the following code:

    C#

    Window w = Application.Current.MainWindow; w.Activate();
  2. Visual Basic

    Dim w As Window = Application.Current.MainWindow w.Activate()
  3. Run the application.
  4. Click the button that forces the notification window to pop up. (It’s easier than adding a new user or event, in order to add another registration.) While the notification popup is visible, bring some other window (e.g. Visual Studio) to the front.
  5. Click on the notification popup. You should see your Silverlight application come back to the foreground.
  6. Show the popup again, and this time, minimize the application window. If you click in the popup, it doesn’t reappear. The taskbar icon will flash to draw attention to the application, but it won’t become visible. To fix this, add the following code to the mouse down handler:

    C#

    if (w.WindowState == WindowState.Minimized) { w.WindowState = WindowState.Normal; }
  7. Visual Basic

    If w.WindowState = WindowState.Minimized Then w.WindowState = WindowState.Normal End If
Note:
When you click on the toast an exception will be thrown. For security reasons you cannot activate a window from a toast unless you are using elevated trust. Otherwise, a user click on a toast could make a OOB application appear with a page that requests Exchange credentials, for example. The next module covers elevated trust scenarios in more depth.

Handling Focus

There’s one more case we’re not handling correctly. If the main window is already active when the notification window appears, the notification ends up stealing the focus.

  1. To fix this, back in the OobUi.xaml.cs or OobUi.xaml.vb code behind, add this line of code at the start of the ShowNotification method:

    C#

    bool mainWindowIsActive = App.Current.MainWindow.IsActive;
  2. Visual Basic

    Dim mainWindowIsActive As Boolean = App.Current.MainWindow.IsActive
  3. Then at the end of the method add this:

    C#

    if (mainWindowIsActive) { App.Current.MainWindow.Activate(); }
  4. Visual Basic

    If mainWindowIsActive Then App.Current.MainWindow.Activate() End If
  5. Run the application again. This time, when notification windows appear, they shouldn’t move the focus away if the main window already has the focus.