Caliburn.Micro icon indicating copy to clipboard operation
Caliburn.Micro copied to clipboard

Please use OnActiveAsync carefully

Open a44281071 opened this issue 3 years ago • 13 comments

We need to implement the following functions:

  • active a screen.
  • open/switch a new view.
  • reload some datas by active/init.

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.

a44281071 avatar Dec 24 '21 02:12 a44281071

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

vb2ae avatar Dec 24 '21 14:12 vb2ae

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

a44281071 avatar Dec 27 '21 03:12 a44281071

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: @.***>

jkattestaart avatar Dec 27 '21 08:12 jkattestaart

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

a44281071 avatar Dec 30 '21 02:12 a44281071

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.

Yinimi avatar Jan 27 '22 08:01 Yinimi

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.
}

a44281071 avatar Jul 14 '22 01:07 a44281071

+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.

theindra avatar Sep 04 '23 02:09 theindra

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...

nietras avatar Sep 07 '23 12:09 nietras

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

vb2ae avatar Sep 10 '23 19:09 vb2ae

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.

nietras avatar Sep 11 '23 07:09 nietras

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

a44281071 avatar Oct 15 '23 10:10 a44281071

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()?

Yinimi avatar Apr 02 '24 12:04 Yinimi

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

a44281071 avatar Apr 03 '24 07:04 a44281071