Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please use OnActiveAsync carefully #789

Open
a44281071 opened this issue Dec 24, 2021 · 13 comments
Open

Please use OnActiveAsync carefully #789

a44281071 opened this issue Dec 24, 2021 · 13 comments

Comments

@a44281071
Copy link

a44281071 commented Dec 24, 2021

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.

@vb2ae
Copy link
Member

vb2ae commented Dec 24, 2021

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

@a44281071
Copy link
Author

a44281071 commented Dec 27, 2021

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

@jkattestaart
Copy link

jkattestaart commented Dec 27, 2021 via email

@a44281071
Copy link
Author

a44281071 commented Dec 30, 2021

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

@Yinimi
Copy link

Yinimi commented Jan 27, 2022

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
Copy link
Author

a44281071 commented Jul 14, 2022

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
Copy link

theindra commented Sep 4, 2023

+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
Copy link

nietras commented Sep 7, 2023

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
Copy link
Member

vb2ae commented Sep 10, 2023

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

@nietras
Copy link

nietras commented Sep 11, 2023

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
Copy link
Author

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

@Yinimi
Copy link

Yinimi commented Apr 2, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants