How to execute command when set the viewModel?

mc 5,426 Reputation points
2023-04-29T11:45:12.9433333+00:00

I set ProductDetailViewModel to bindingContext of ProductDetail

I want it auto run function that get the detail.

public async Task GetProductDetailAsync()
{
}

int the ViewModel

can it auto or I have to run it in onAppearing?

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2023-04-29T12:13:17.1+00:00

    You can run it on the viewmodel constructor with a Task.Run(async()=> await GetProductDetailAsync)

    Or you can run in the OnAppearing. OnAppearing is an event so you can convert to a command using EventToCommandBehaviour in the CommunityToolkit.Maui

    
        <ContentPage.Behaviors>
            <mct:EventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing" />
            <mct:EventToCommandBehavior Command="{Binding DisappearingCommand}" EventName="Disappearing" />
        </ContentPage.Behaviors>
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.