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

Screen.OnInitializeAsync prevents the view from getting displayed immediately #591

Closed
mgnsm opened this issue Mar 24, 2019 · 1 comment
Closed

Comments

@mgnsm
Copy link
Contributor

mgnsm commented Mar 24, 2019

The OnInitializeAsync method of the Screen class "blocks" the view from getting display until the view model has been initialized. This is not the expected behaviour as it basically defeats the purpose of initializing the view model asynchronously in the first place. The issue is easy to reproduce in a WPF project that references the 4.0.47-alpha version.

ShellView:

<TabControl x:Name="Items" Margin="10">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

ShellViewModel:

public class ShellViewModel : Conductor<IScreen>.Collection.AllActive
{
    public ShellViewModel()
    {
        Items.Add(new ChildViewModel() { DisplayName = "Child..." });
    }
}

ChildViewModel:

public class YmerViewModel : Screen
{
    protected override async Task OnInitializeAsync(CancellationToken cancellationToken)
    {
       //simulate some initialization work...
       await Task.Run(() => Thread.Sleep(5000));
    }
}

Bootstrapper:

public class Bootstrapper : BootstrapperBase
{
    public Bootstrapper() => Initialize();

    protected override void OnStartup(object sender, StartupEventArgs e) =>
        DisplayRootViewFor<ShellViewModel>(s_windowSettings);
}

Using the above sample code, it takes five seconds for the main window to show up. This can be fixed by modifying the ChangeActiveItemAsync method of the ConductorBaseWithActiveItem<T> class slightly. I'll submit a pull request.

nigel-sampson added a commit that referenced this issue Mar 24, 2019
#591 Display the view *before* activating the view model asynchronously.
@nigel-sampson
Copy link
Contributor

Thanks for the PR.

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

2 participants