/// <summary>"Catches" a new dialog opened (if any) during executions of <paramref name="action"/>.</summary> /// <param name="clientSession">The client Session.</param> /// <param name="action">The action.</param> /// <returns>The catch dialog. If no such dialog exists, returns null.</returns> public static ClientLogicalForm CatchDialog(this ClientSession clientSession, Action action) { ClientLogicalForm dialog = null; EventHandler <ClientDialogToShowEventArgs> clientSessionOnDialogToShow = (sender, args) => dialog = args.DialogToShow; bool wasSuspended = false; // If there is an instance of UnexpectedDialogHandler registered in the client session // make sure the UnexpectedDialogHandler is suspended while we catch a dialog UnexpectedDialogHandler unexpectedDialogHandler = GetUnexpectedDialogHandler(clientSession); if (unexpectedDialogHandler != null) { wasSuspended = unexpectedDialogHandler.IsSuspended; unexpectedDialogHandler.IsSuspended = true; } clientSession.DialogToShow += clientSessionOnDialogToShow; try { action(); } finally { clientSession.DialogToShow -= clientSessionOnDialogToShow; if (unexpectedDialogHandler != null) { unexpectedDialogHandler.IsSuspended = wasSuspended; } } return(dialog); }
private static UnexpectedDialogHandler GetUnexpectedDialogHandler(ClientSession clientSession) { object dialogHandlerObj; if (clientSession.Attributes.TryGetValue(UnexpectedDialogHandler.UnexpectedDialogHandlerKey, out dialogHandlerObj)) { UnexpectedDialogHandler unexpectedDialogHandler = dialogHandlerObj as UnexpectedDialogHandler; if (unexpectedDialogHandler != null) { return(unexpectedDialogHandler); } } return(null); }