Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.79k stars 776 forks source link

Please use OnActiveAsync carefully #789

Open a44281071 opened 2 years ago

a44281071 commented 2 years ago

We need to implement the following functions:

We can't use OnActiveAsync to load data. This will Put off the view display before async method finish. The solution is OnViewLoaded, BUT it can't await.

vb2ae commented 2 years ago

@a44281071 are you asking for OnViewLoaded to be an async event?

a44281071 commented 2 years ago

@vb2ae Maybe something like "Before init/active" and "After init/active" is comfortable. Any better ideas?

jkattestaart commented 2 years ago

I always stayed away from de the activate stuff because it was not async in previous versions and created a StartAsync Method in my BaseViewModel. Only drawback is that i have to implicit call the StartAsync

Reagrds

John Kattestaart @.***

Op ma 27 dec. 2021 om 04:58 schreef SamTi(三台) @.***>:

@vb2ae https://github.com/vb2ae Maybe something like "Before init/activ" and "After init/active" is comfortable. Any better idea?

— Reply to this email directly, view it on GitHub https://github.com/Caliburn-Micro/Caliburn.Micro/issues/789#issuecomment-1001318415, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACULQHSWRDHHFBXZTCH3KQLUS7P6TANCNFSM5KV3ZUOQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

a44281071 commented 2 years ago

Perhaps we need a series of tutorials to demonstrate the best way to use the new asynchronous methods (active, deactive, handle event)

Yinimi commented 2 years ago

One problem I've found with OnActivateAsync is, it is called just before IsActive is set to true (see Screen.ActivateAsync()). This means if you have a main viewmodel that uses OnActivateAsync to initialize and activate some sub viewmodel like a login viewmodel then the OnActivateAsync in the sub viewmodel will be skipped ConductorBaseWithActiveItem.ChangeActiveItemAsync(). In this scenario it will never be called.

Edit: Replaced the second link with the actual method where it is skipped.

a44281071 commented 2 years ago

Please add some override methods for Active/Deactive. Like BeforActiveAsync/AfterActiveAsync..

Conductor method ActivateItemAsync() ambiguity.


protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
{
    var box1 = new BoxViewModel(new MaterialViewModel { DisplayName = "child 111." });
    await ActivateItemAsync(box1);
    // box1 didn't active before this parent actived.
    await base.OnInitializeAsync(cancellationToken);
}

public async Task ClickButtonAddChildAsync()
{
    var box2 = new BoxViewModel(new MaterialViewModel { DisplayName = "child 222." });
    await ActivateItemAsync(box2);
    // box2 actived because this parent actived.
}
theindra commented 1 year ago

+1 to that.

How can I activate a sub item from within OnActivateAsync of the parent? I just upgraded from Caliburn 3.2 to 4 and this breaks.

nietras commented 1 year ago

I'm having a similar issue migrating from 3.x to 4.x and we used to use OnInitialize to do lots of things that would be displayed, now nothing is displayed until initialize is returned, seems like a completely different way to do this. And OnActivateAsync can't be used either since nothing is displayed either before it returns. Before 4.x in 3.x view was displayed before OnInitialize called as far as I can tell and would update if VM/UI changes. Now I can't do this unless using OnViewLoaded which then isn't async/Task so it will block...

vb2ae commented 1 year ago

What are you doing in the OnInitialize? trying to get an idea on what the use case is

nietras commented 1 year ago

What are you doing in the OnInitialize? trying to get an idea on what the use case is

For example, running various async tasks like loading things from file etc. Things we want to show progress for.

a44281071 commented 11 months ago

Or load data from webapi/db/remote-io.

Yinimi commented 5 months ago

with my PR #898 the second method for Initialize is added as well. Since we have now the OnActivated() is there still a need for a async OnViewLoaded()?

a44281071 commented 5 months ago

Like asp.net Blazor, It have InitAsync() and AfterAsync(bool firstRender). Used for 'Load remote data/webapi, init(bind) JS module'.