microsoft / TemplateStudio

Template Studio accelerates the creation of new WinUI 3, WPF, and UWP apps using a wizard-based experience.
Other
2.68k stars 459 forks source link

Dev-nightly: Prism app crashes when adding LiveTile and MultiView #2533

Closed sibille closed 6 years ago

sibille commented 6 years ago

Repro steps:

  1. Create a project of type SplitView and framework Prism
  2. Add MultiView and LiveTile Feature (in this order)
  3. Start the app

Result: App crashes with error "Element not found. (Exception from HRESULT: 0x80070490)" at Windows.UI.ViewManagement.ApplicationView.GetForCurrentView()

Version: Templates version: 0.18.18242.2 Wizard version: 0.18.18242.1

sibille commented 6 years ago

The generated code on App.xaml.cs is

protected override async Task OnInitializeAsync(IActivatedEventArgs args)
{
    await Container.Resolve<ILiveTileService>().EnableQueueAsync().ConfigureAwait(false);
    WindowManagerService.Current.Initialize();
    await ThemeSelectorService.InitializeAsync().ConfigureAwait(false);

    // We are remapping the default ViewNamePage and ViewNamePageViewModel naming to ViewNamePage and ViewNameViewModel to
    // gain better code reuse with other frameworks and pages within Windows Template Studio
    ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
    {
        var viewModelTypeName = string.Format(CultureInfo.InvariantCulture, "App109.ViewModels.{0}ViewModel, App109", viewType.Name.Substring(0, viewType.Name.Length - 4));
        return Type.GetType(viewModelTypeName);
     });
     await base.OnInitializeAsync(args);
}

Changing the execution order to the following solves the problem and would actually be better, as ThemeSelectorSevice should be initialized early to ensure correct theme is shown from the start

protected override async Task OnInitializeAsync(IActivatedEventArgs args
{
    WindowManagerService.Current.Initialize();
    await ThemeSelectorService.InitializeAsync().ConfigureAwait(false);

    // We are remapping the default ViewNamePage and ViewNamePageViewModel naming to ViewNamePage and ViewNameViewModel to
    // gain better code reuse with other frameworks and pages within Windows Template Studio
    ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
    {
        var viewModelTypeName = string.Format(CultureInfo.InvariantCulture, "App109.ViewModels.{0}ViewModel, App109", viewType.Name.Substring(0, viewType.Name.Length - 4));
        return Type.GetType(viewModelTypeName);
    });

    await Container.Resolve<ILiveTileService>().EnableQueueAsync().ConfigureAwait(false);

    await base.OnInitializeAsync(args);
}

@LeeParrishMSFT and @milazzom, I'll prepare a PR for this, could you review?

milazzom commented 6 years ago

@sibille I'm checking it out.

crutkas commented 6 years ago

Talking with @milazzom and @sibille, @milazzom has a more robust solution here. We should do that along with the reordering.

I suggest we add a comment for these two lines of code for all frameworks that basically is // These are initializing the window and setting the theme, they should be the first two items done else you may get a flickering for setting theming / delay in window creation. WindowManagerService.Current.Initialize(); await ThemeSelectorService.InitializeAsync().ConfigureAwait(false);

sibille commented 6 years ago

@crutkas, you can have multiview and themeselection or any of the two separately, we'll have to do two separate comments, like this:

//This is initializing the window manager service, make sure this is one of the first calls to avoid delay in window creation WindowManagerService.Current.Initialize(); //This is setting the theme, make sure this is one of the first calls to avoid flickering. await ThemeSelectorService.InitializeAsync().ConfigureAwait(false);

sibille commented 6 years ago

Verified in dev-nightly: Templates version: 0.18.18255.1 Wizard version: 0.18.18255.1