Current filter:
                                You should refresh the page.
                                  • Hello,
                                    so I only recently started using MVVM in a project I'm working on and even though I read your documentation I'm having trouble finding a way to:
                                    - first, cancel a command other than with a binding . I'm currently using a trigger to fire a command upon change of a property from the ViewModel. Since that property is currently being filled by an event I can also use EventToCommand.

                                    fluentAPI.WithEvent(myGridControl, nameof(myGridControl.FocusedRowChanged)) .SetBinding(x => x.MyProperty, e => myGridControl.FocusedRow); //fluentAPI.SetTrigger(x => x.MyProperty, (x, a) => x.MyAsyncCommand()); fluentAPI.WithEvent<FocusedRowChangedEventArgs>(myGridControl, nameof(myGridControl.FocusedRowChanged)) .EventToCommand(x => x.MyAsyncCommand());

                                    (note that myGridControl is a custom control that has a GridControl and provides a property named FocusedRow that calls GridView.GetFocusedRow() )

                                    - second, restart the command immediately after canceling the previous one when the focused row has changed.
                                    That asynchronous method retrieves some files (which need to be copied if not yet available) that are linked to a row and display them in a control that can preview them. I have to provide the files to that control using a DispatcherService plus a custom PreviewService that has access to the preview control ins question. It works fine most of the time except if you quickly click a row before the task has finished for the previous one. In that case my preview control will list all the files linked to both rows.

                                    Now I know how to retrieve the current command inside of its own code and check for cancellation but that never occurs automatically. So I guess I have to do it manually. I don't know how to bind a cancellation command other than with a button or something similar (which requires manual user interaction). I tried retrieving the CancelCommand from the result of a call to GetAsyncCommand() and execute it but it does nothing. Probably because there is (apparently) no CancellationTokenSource associated with the command. And since I suppose your MVVM Framework handles the creation of tokens I think I don't have to provide it manually but rather am missing someting that I couldn't find in your documentation.
                                    Maybe I'm missing an abvious thing but this is a critical bug for the users of the application since the documents they see determine what they do.
                                    Thanks for the help.

                                1 Solution

                                Creation Date Importance Sort by

                                Hello Grégory,
                                Asynchronous commands should be canceled manually. For this, you can mark a command with the AsyncCommand attribute with the AllowMultipleExecution flag set to True:

                                [AsyncCommand(AllowMultipleExecution = true, UseCommandManager = false)] public Task MyTestMethod() { return Task.Factory.StartNew(CalculateCore, command.CancellationTokenSource.Token); }

                                Then, you can obtain the CancellationTokenSource as follows:

                                command = this.GetAsyncCommand(x => x.MyTestMethod()); var token = command.CancellationTokenSource;

                                I've prepared a small sample project showing this approach.
                                P.S. We have the Asynchronous-Commands help article that illustrates this approach. We will consider adding a similar article for the WinForms section.
                                Should you have further questions, let me know.