internal CommandMediator( Window window, Interaction interaction, bool bindCancelToClose ) { Contract.Requires( window != null ); this.window = window; this.window.Closing += OnWindowClosing; if ( interaction == null ) return; commands = interaction.Commands; commands.ForEach( c => c.Executed += OnExecuted ); if ( !bindCancelToClose ) return; // determine if behavior is already applied var behaviors = System.Windows.Interactivity.Interaction.GetBehaviors( this.window ); var behavior = behaviors.OfType<WindowCloseBehavior>().FirstOrDefault(); // add behavior which can support binding the cancel command to the window close button (X) if ( behavior == null ) { behavior = new WindowCloseBehavior(); behaviors.Add( behavior ); } behavior.CloseCommand = interaction.CancelCommand; }
public void TitleShouldNotAllowNull() { // arrange var interaction = new Interaction(); // act var ex = Assert.Throws<ArgumentNullException>( () => interaction.Title = null ); // assert Assert.Equal( "value", ex.ParamName ); }
public void ConstructorShouldSetInteraction() { // arrange var expected = new Interaction(); var args = new InteractionRequestedEventArgs( expected ); // act var actual = args.Interaction; // assert Assert.Same( expected, actual ); }
public void TitleShouldWriteExpectedValue() { // arrange var expected = "Test"; var interaction = new Interaction(); // act Assert.PropertyChanged( interaction, "Title", () => interaction.Title = expected ); var actual = interaction.Title; // assert Assert.Equal( expected, actual ); }
public void RequestShouldRaiseEvent() { // arrange var expected = new Interaction(); var request = new InteractionRequest<Interaction>(); Interaction actual = null; request.Requested += ( s, e ) => actual = e.Interaction; // act request.Request( expected ); // assert Assert.Same( expected, actual ); }
protected virtual IAsyncOperation<IUICommand> AlertAsync( Interaction interaction ) { Arg.NotNull( interaction, nameof( interaction ) ); Contract.Ensures( Contract.Result<IAsyncOperation<IUICommand>>() != null ); var content = interaction.Content == null ? string.Empty : interaction.Content.ToString(); var dialog = new MessageDialog( content, interaction.Title ); dialog.DefaultCommandIndex = 0; if ( interaction.Commands.Count == 0 ) dialog.Commands.Add( new UICommand( SR.OKCaption, DefaultAction.None ) ); else dialog.Commands.Add( interaction.Commands[0].AsUICommand() ); return dialog.ShowAsync(); }
protected virtual IAsyncOperation<IUICommand> PromptAsync( Interaction interaction ) { Arg.NotNull( interaction, nameof( interaction ) ); Contract.Ensures( Contract.Result<IAsyncOperation<IUICommand>>() != null ); var content = interaction.Content == null ? string.Empty : interaction.Content.ToString(); var dialog = new MessageDialog( content, interaction.Title ); if ( interaction.DefaultCommandIndex >= 0 ) dialog.DefaultCommandIndex = (uint) interaction.DefaultCommandIndex; if ( interaction.CancelCommandIndex >= 0 ) dialog.CancelCommandIndex = (uint) interaction.CancelCommandIndex; dialog.Commands.AddRange( interaction.Commands.Select( c => c.AsUICommand() ) ); return dialog.ShowAsync(); }
public void ExecuteDefaultCommandShouldExecuteDisabledCommand() { // arrange var command = new Mock<INamedCommand>(); command.Setup( c => c.CanExecute( It.IsAny<object>() ) ).Returns( false ); command.Setup( c => c.Execute( It.IsAny<object>() ) ); var interaction = new Interaction() { DefaultCommandIndex = 0, Commands = { command.Object } }; // act interaction.ExecuteDefaultCommand(); // assert command.Verify( c => c.Execute( null ), Times.Never() ); }
public static Task AlertAsync( this InteractionRequest<Interaction> interactionRequest, string title, string message, string acknowledgeButtonText ) { Arg.NotNull( interactionRequest, nameof( interactionRequest ) ); if ( string.IsNullOrEmpty( acknowledgeButtonText ) ) acknowledgeButtonText = SR.OK; var source = new TaskCompletionSource<object>(); var interaction = new Interaction( title ?? string.Empty, message ?? string.Empty ) { DefaultCommandIndex = 0, Commands = { new NamedCommand<object>( "OK", acknowledgeButtonText, p => source.TrySetResult( null ) ) } }; interactionRequest.Request( interaction ); return source.Task; }
/// <summary> /// Initializes a new instance of the <see cref="InteractionRequestedEventArgs"/> class. /// </summary> /// <param name="interaction">The <see cref="Interaction">interaction</see> being requested.</param> public InteractionRequestedEventArgs( Interaction interaction ) { Arg.NotNull( interaction, nameof( interaction ) ); this.interaction = interaction; }
public void CancelCommandIndexShouldWriteExpectedValue() { // arrange var expected = 0; var interaction = new Interaction(); // act Assert.PropertyChanged( interaction, "CancelCommandIndex", () => interaction.CancelCommandIndex = expected ); var actual = interaction.CancelCommandIndex; // assert Assert.Equal( expected, actual ); }
public void ExecuteCancelCommandShouldExecuteExpectedCommand() { // arrange var command = new Mock<INamedCommand>(); command.Setup( c => c.CanExecute( It.IsAny<object>() ) ).Returns( true ); command.Setup( c => c.Execute( It.IsAny<object>() ) ); var interaction = new Interaction() { CancelCommandIndex = 0, Commands = { command.Object } }; // act interaction.ExecuteCancelCommand(); // assert command.Verify( c => c.Execute( null ), Times.Once() ); }
private void OnBrowseForViewModel( object parameter ) { if ( !OnCanBrowseForViewModel( parameter ) ) return; var interaction = new Interaction() { Title = SR.ExistingViewModelTitle, DefaultCommandIndex = 0, CancelCommandIndex = 1, Commands = { new NamedCommand<object>( SR.OKCaption, DefaultAction.None ), new NamedCommand<object>( SR.CancelCaption, p => ViewModelType = null ) } }; // request an ui interaction to browse for a type browse.Request( interaction ); }
public static Task<bool> ConfirmAsync( this InteractionRequest<Interaction> interactionRequest, string prompt, string title, string acceptButtonText, string cancelButtonText ) { Arg.NotNullOrEmpty( prompt, nameof( prompt ) ); if ( string.IsNullOrEmpty( acceptButtonText ) ) acceptButtonText = SR.OK; if ( string.IsNullOrEmpty( cancelButtonText ) ) cancelButtonText = SR.Cancel; var source = new TaskCompletionSource<bool>(); var interaction = new Interaction() { Title = title ?? string.Empty, Content = prompt, DefaultCommandIndex = 0, CancelCommandIndex = 1, Commands = { new NamedCommand<object>( "OK", acceptButtonText, p => source.TrySetResult( true ) ), new NamedCommand<object>( "Cancel", cancelButtonText, p => source.TrySetResult( false ) ) } }; interactionRequest.Request( interaction ); return source.Task; }
public void CancelCommandShouldReturnExpectedCommand() { // arrange var expected = new Mock<INamedCommand>().Object; var interaction = new Interaction() { Commands = { new Mock<INamedCommand>().Object, new Mock<INamedCommand>().Object, expected } }; // act Assert.PropertyChanged( interaction, "CancelCommand", () => interaction.CancelCommandIndex = 2 ); var actual = interaction.CancelCommand; // assert Assert.Same( expected, actual ); }
private void PrepareDialog( ContentDialog dialog, Interaction interaction ) { Contract.Requires( dialog != null ); Contract.Requires( interaction != null ); if ( ContentTemplate != null ) dialog.ContentTemplate = ContentTemplate; if ( TitleTemplate != null ) dialog.TitleTemplate = TitleTemplate; if ( Style != null ) dialog.Style = Style; // use interaction if there is no content if ( dialog.Content == null ) dialog.Content = interaction.Content ?? interaction; // dialog might already have its own view model, if it doesn't // use the interaction as the model if ( dialog.DataContext == null ) dialog.DataContext = interaction; }
public void ContentShouldWriteExpectedValue() { // arrange var expected = new object(); var interaction = new Interaction(); // act Assert.PropertyChanged( interaction, "Content", () => interaction.Content = expected ); var actual = interaction.Content; // assert Assert.Same( expected, actual ); }
public void ConstructorShouldSetContent() { // arrange var expected = new object(); // act var interaction = new Interaction( "Test", expected ); var actual = interaction.Content; // assert Assert.Same( expected, actual ); }