Skip to content

Commit

Permalink
fix(uno): Fix Windows initialization sequence
Browse files Browse the repository at this point in the history
This change takes into account the fact that the visual tree cannot be navigated until it is loaded. This fixes PrismLibrary#2102.
  • Loading branch information
jeromelaban committed Apr 30, 2020
1 parent 3ee01c9 commit e06a353
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 10 deletions.
4 changes: 4 additions & 0 deletions PrismLibrary_Uno.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Core", "src\Prism.Core\Prism.Core.csproj", "{CF74BE80-F9A0-4C02-A59C-30F43EDAB866}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Containers\Prism.Unity.Shared\Prism.Unity.Shared.projitems*{4338e214-2ba3-4043-90b6-6c1269c58865}*SharedItemsImports = 5
src\Containers\Prism.DryIoc.Shared\Prism.DryIoc.Shared.projitems*{b09fc94c-8100-4eb2-846c-99acb056a993}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
Ad-Hoc|ARM = Ad-Hoc|ARM
Expand Down
9 changes: 9 additions & 0 deletions e2e/Uno/HelloUnoWorld.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Windows.UI.Xaml.Navigation;
using Prism.Modularity;
using HelloWorld.ViewModels;
using Prism.Regions;

namespace HelloUnoWorld
{
Expand Down Expand Up @@ -117,9 +118,17 @@ protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
moduleCatalog.AddModule<ModuleA.ModuleAModule>(InitializationMode.OnDemand);
}

protected override void OnInitialized()
{
base.OnInitialized();
var regionManager = Container.Resolve<IRegionManager>();
regionManager.RequestNavigate("InitialRegion", nameof(InitialView));
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>(nameof(ViewA));
containerRegistry.RegisterForNavigation<InitialView>(nameof(InitialView));
containerRegistry.RegisterForNavigation<ModulesPage, ModulesPageViewModel>();

containerRegistry.RegisterDialog<NotificationDialog, NotificationDialogViewModel>();
Expand Down
9 changes: 8 additions & 1 deletion e2e/Uno/HelloUnoWorld.Shared/HelloUnoWorld.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<SharedGUID>6279c845-92f8-4333-ab99-3d213163593c</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>HelloUnoWorld.Shared</Import_RootNamespace>
<Import_RootNamespace>HelloUnoWorld</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
Expand All @@ -32,6 +32,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Dialogs\NotificationDialogViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\ModulesPageViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\ShellViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Views\InitialView.xaml.cs">
<DependentUpon>InitialView.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Views\ModulesPage.xaml.cs">
<DependentUpon>ModulesPage.xaml</DependentUpon>
</Compile>
Expand All @@ -55,6 +58,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Views\InitialView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Views\ModulesPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
15 changes: 15 additions & 0 deletions e2e/Uno/HelloUnoWorld.Shared/Views/InitialView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl
x:Class="HelloUnoWorld.Views.InitialView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HelloUnoWorld.Shared.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
<TextBlock x:Name="InitialViewTextBlock" FontSize="20" Text="Initial view" />
</Grid>
</UserControl>
27 changes: 27 additions & 0 deletions e2e/Uno/HelloUnoWorld.Shared/Views/InitialView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace HelloUnoWorld.Views
{
public sealed partial class InitialView : UserControl
{
public InitialView()
{
this.InitializeComponent();
}
}
}
15 changes: 11 additions & 4 deletions e2e/Uno/HelloUnoWorld.Shared/Views/Shell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
xmlns:toolkit="using:Uno.UI.Toolkit"
xmlns:local="clr-namespace:HelloWorld">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" toolkit:VisibleBoundsPadding.PaddingMask="All">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
toolkit:VisibleBoundsPadding.PaddingMask="All">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" FontSize="30" />
<StackPanel Grid.Row="1" Orientation="Vertical">
<TextBlock Text="{Binding Title}"
FontSize="30" />
<StackPanel Grid.Row="1"
Orientation="Vertical">
<Button x:Name="viewAButton"
Command="{Binding NavigateCommand}"
CommandParameter="ViewA"
Expand All @@ -31,8 +36,10 @@
Command="{Binding NavigateCommand}"
CommandParameter="ModulePageA"
Content="Navigate to Named View in Module" />
</StackPanel>
</StackPanel>
<ContentControl Grid.Row="2"
prismRegions:RegionManager.RegionName="InitialRegion" />
<ContentControl Grid.Row="3"
prismRegions:RegionManager.RegionName="ContentRegion" />
</Grid>
</ContentControl>
11 changes: 11 additions & 0 deletions e2e/Uno/HelloUnoWorld.UITests/Region_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ namespace Sample.UITests
{
public class Region_Tests : TestBase
{
[Test]
public void InitialView()
{
Query initialView = q => q.Marked("InitialViewTextBlock");

App.WaitForElement(initialView);
TakeScreenshot("Initial");

App.WaitForDependencyPropertyValue(initialView, "Text", "Initial view");
}

[Test]
public void TestViewA()
{
Expand Down
45 changes: 40 additions & 5 deletions src/Uno/Prism.Uno/PrismApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void InitializeInternal()
{
ConfigureViewModelLocator();
Initialize();
OnInitialized();
}

/// <summary>
Expand Down Expand Up @@ -90,12 +89,48 @@ public virtual void Initialize()
if (shell != null)
{
_containerExtension.Resolve<IRegionNavigationService>().NavigationFailed += (s, e) => Console.WriteLine($"Region navigation failed {e.Error}");
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();
InitializeShell(shell);
}

InitializeModules();
void FinalizeInitialization()
{
RegionManager.SetRegionManager(shell, _containerExtension.Resolve<IRegionManager>());
RegionManager.UpdateRegions();

InitializeModules();
OnInitialized();
}

if (shell is FrameworkElement fe)
{
void OnLoaded(object sender, object args)
{
FinalizeInitialization();
fe.Loaded -= OnLoaded;
}

#if HAS_UNO
// Uno currently loads items earlier than UWP, so we can check
// for the IsLoaded property. UWP got that property in SDK 17763,
// meaning that the condition can be removed once the SDK is updated
// in Prism.Uno.
if (fe.IsLoaded)
{
FinalizeInitialization();
}
else
#endif
{
// We need to delay the initialization after the shell has been loaded, otherwise
// the visual tree is not materialized for the RegionManager to be available.
// See https://github.com/PrismLibrary/Prism/issues/2102 for more details.
fe.Loaded += OnLoaded;
}
}
else
{
FinalizeInitialization();
}
}
}

/// <summary>
Expand Down

0 comments on commit e06a353

Please sign in to comment.