public async Task CatchesUnhandledExceptionFromTask() { var delay = Task.Delay(200); string message = null; ExecutionAwareCommand command = null; var source = new TaskCompletionSource <bool>(); command = ExecutionAwareCommand.FromTask(async() => { await delay; source.SetResult(true); throw new Exception("Test"); }) .Catch(e => message = e.Message); var ex = Record.Exception(() => ((ICommand)command).Execute(null)); await source.Task; // add some extra delay to ensure that source task has fully completed including the Completion Task. await Task.Delay(200); Assert.False(command.IsExecuting); Assert.Null(ex); Assert.Equal("Test", message); }
public BlankViewModel(INavigationService navigationService) : base(navigationService) { //Create commands LoadCommand = ExecutionAwareCommand.FromTask(LoadDatas).OnIsExecutingChanged(SetIsExecuting); //Subscribe to Properties changes this.WhenAnyValue(x => x.Property1).Subscribe(Property1Changed).DisposeWith(DestroyWith); }
public WalkThroughViewModel(INavigationService navigationService) : base(navigationService) { NavigateOutCommand = ExecutionAwareCommand.FromTask(NavigateOutAsync) .OnIsExecutingChanged(SetIsExecuting); InitWT(); this.WhenAnyValue(x => x.IndicatorPosition).Subscribe(EvaluatePosition).DisposeWith(DestroyWith); }
public void SuppressesInvalidCast() { int number = 1; ICommand command = ExecutionAwareCommand.FromAction <int>(i => number = i) .SetThrowOnInvalidCast(false); var ex = Record.Exception(() => command.Execute("Hello World")); Assert.Null(ex); Assert.Equal(0, number); }
protected ViewModelBasePage(INavigationService navigationService) : base(navigationService) { try { NavigateBackCommand = ExecutionAwareCommand.FromTask(NavigateBackAsync) .OnIsExecutingChanged(SetIsExecuting); } catch (Exception ex) { Logger.Write(ex); } }
public void ThrowsOnInvalidCast() { int number = 0; ICommand command = ExecutionAwareCommand.FromAction <int>(i => number = i) .SetThrowOnInvalidCast(true); var ex = Record.Exception(() => command.Execute("Hello World")); Assert.NotNull(ex); Assert.IsType <UnhandledCommandException>(ex); Assert.IsType <InvalidCastException>(ex.InnerException); }
public void IsExecutingIsChanged() { bool didExecute = false; bool didCommandIsExecutingChange = false; ICommand command = ExecutionAwareCommand.FromAction(() => didExecute = true) .OnIsExecutingChanged(_ => didCommandIsExecutingChange = true); command.Execute(null); Assert.True(didExecute); Assert.True(didCommandIsExecutingChange); }
public MDViewModel(INavigationService navigationService /*, INotificationService notificationService*/) : base(navigationService) { //_notificationService = notificationService; //Create commands LogoutCommand = ExecutionAwareCommand.FromTask(LogoutUser).OnIsExecutingChanged(SetIsExecuting); //MenuNavigateCommand = ExecutionAwareCommand.FromTask(MenuNavigateTo).OnIsExecutingChanged(OnIsExecutingChanged); //Subscribe to Properties changes this.WhenAnyValue(x => x.SelectedMenuItem).Subscribe(MenuNavigateTo).DisposeWith(DestroyWith); Task.Run(() => LoadDatas()); }
public void ProperlyCatchesThrownInvalidCast() { int number = 0; Exception caughtException = null; ICommand command = ExecutionAwareCommand.FromAction <int>(i => number = i) .SetThrowOnInvalidCast(true) .Catch(e => caughtException = e); var ex = Record.Exception(() => command.Execute("Hello World")); Assert.Null(ex); Assert.NotNull(caughtException); Assert.IsType <InvalidCastException>(caughtException); }
public MainPageViewModel(INavigationService navigationService, IApizrManager <IReqResService> reqResManager, IApizrManager <ICrudApi <User, int, PagedResult <User>, IDictionary <string, object> > > userCrudManager, IApizrManager <IHttpBinService> httpBinManager, IApizrManager <ICrudApi <UserDetails, int, IEnumerable <UserDetails>, IDictionary <string, object> > > userDetailsCrudManager, IMediator mediator, ICrudOptionalMediator <User, int, PagedResult <User>, IDictionary <string, object> > userOptionalMediator) : base(navigationService) { _reqResManager = reqResManager; _userCrudManager = userCrudManager; _httpBinManager = httpBinManager; _userDetailsCrudManager = userDetailsCrudManager; _mediator = mediator; _userOptionalMediator = userOptionalMediator; GetUsersCommand = ExecutionAwareCommand.FromTask(GetUsersAsync); //GetUserDetailsCommand = ExecutionAwareCommand.FromTask<User>(GetUserDetails); GetUserDetailsCommand = new DelegateCommand <User>(async user => await GetUserDetails(user)); AuthCommand = ExecutionAwareCommand.FromTask(AuthAsync); Title = "Main Page"; }
protected ViewModelBase(INavigationService navigationService) { try { NavigationService = navigationService; SettingsService = ShinyHost.Resolve <ISettingsService>(); LocalizationManager = ShinyHost.Resolve <ILocalizrManager>(); DialogsService = ShinyHost.Resolve <IDialogService>(); NavigateBackCommand = ExecutionAwareCommand.FromTask(NavigateBackAsync) .OnIsExecutingChanged(OnIsExecutingChanged); } catch (Exception ex) { Logger.Write(ex); } }
public async Task IsExecutingChangedWhileTaskCompletes() { bool isExecuting = false; var delay = Task.Delay(1000); var propertiesChanged = new List <string>(); ExecutionAwareCommand command = null; var source = new TaskCompletionSource <bool>(); command = ExecutionAwareCommand.FromTask(async() => { isExecuting = true; await delay; source.SetResult(true); }); Assert.Null(command.ExecuteDelegate); Assert.NotNull(command.AsyncExecuteDelegate); void PropertyChanged(object sender, PropertyChangedEventArgs args) { propertiesChanged.Add(args.PropertyName); } command.PropertyChanged += PropertyChanged; ((ICommand)command).Execute(null); Assert.True(command.IsExecuting); Assert.True(isExecuting); await source.Task; // add some extra delay to ensure that source task has fully completed including the Completion Task. await Task.Delay(200); Assert.False(command.IsExecuting); command.PropertyChanged -= PropertyChanged; Assert.Equal(2, propertiesChanged.Where(x => x == "IsExecuting").Count()); }