Implementing a Modal Dialog in Silverlight 5

I'm looking for a good way to implement a truly modal dialog in Silverlight 5. Every example I find that the claims to create a modal dialog isn't really modal in that the calling code waits until the dialog is closed.

I understand this is a call because we cannot actually block the UI thread, because it has to work for the (ChildWindow) dialog to function properly. But with the addition of TPLs in SL5 and the higher adoption of Silverlight over the past few years, I hope someone has found a way around this.

A good representative scenario I'm trying to solve is an action (like clicking a button or a menu item) that displays a login dialog and must wait for registration to complete before proceeding.

In our particular business case (whether logical or not), the application does not require user authentication; however, some features require Manager access. When a function is available (by clicking a button or a selected menu item, etc.) and the current user is not a manager, we display the login dialog. When the dialog closes, we check the user's authorization again. If they are not logged in, we show a good message and reject the action. If they are authorized, we continue to perform the action, which usually involves changing the current view to something new, where the user can do whatever they asked for.

+3


source to share


1 answer


To close ...

As a result, I have a new TPL-enabled DialogService with methods like:

public Task<Boolean?> ShowDialog<T>()
    where T : IModalWindow

      

The method creates an instance of the dialog (ChildWindow), attaches a handler to the private event, and calls the Show method. Internally it uses TaskCompletionSource <Boolean? > to return a Task to the caller. In the Closed event handler, the DialogResult is passed to the TrySetResult () method of the TaskCompletionSource object.



This allows me to display the dialog in the usual async fashion:

DialogService.ShowDialog<LoginDialog>().ContinueWith(task =>
{
    var result = task.Result;

    if ((result == true) && UserHasPermission())
    {
        // Continue with operation
    }
    else
    {
        // Display unauthorized message
    }
}, TaskScheduler.FromCurrentSynchronizationContext());

      

Or I could block the use of the Task.Wait () method, although this is problematic because, as I mentioned in the original post, it blocks the UI thread, so even the dialog is frozen.

Still not a real modal dialog, but a little closer to the behavior I'm looking for. Any improvements or suggestions are still appreciated.

+5


source







All Articles