Skip to content

Does RaiseCanExecuteChanged need to be called on the UI thread? #78

Answered by brminnick
CodingOctocat asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @CodingOctocat!

The answer is "It Depends".

Non UI Frameworks

No, you do not need to marshall RaiseCanExecuteChanged to the main thread

UI Frameworks

If you are using a UI dependent framework, such as WinForms, WPF, Xamarin, etc.,

AND

ICommand is bound to a UI control (eg a Button), then yes you will need to invoke RaiseCanExecuteChanged on the main thread:

Xamarin Example

public IAsyncCommand ButtonCommand { get; } = new AsyncCommand(ExecuteButtonCommand);

static async Task ExecuteButtonCommand()
{
    // Handle Button Command
}

void OnCanExecuteChanged() => Device.BeginInvokeOnMainThread(ButtonCommand.RaiseCanExecuteChanged);

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@CodingOctocat
Comment options

@brminnick
Comment options

@CodingOctocat
Comment options

@brminnick
Comment options

@CodingOctocat
Comment options

Answer selected by CodingOctocat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #77 on June 08, 2021 16:25.