From e36bb3d10fc199342dd833518e9cd6970c4e7431 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Wed, 26 Oct 2022 12:40:16 +1100 Subject: [PATCH] feat: Moving navigation to background thread --- src/Uno.Extensions.Navigation.UI/Navigator.cs | 1 + .../Navigators/PanelVisiblityNavigator.cs | 27 ++++++++++--------- .../NavigationRequest.cs | 4 ++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Uno.Extensions.Navigation.UI/Navigator.cs b/src/Uno.Extensions.Navigation.UI/Navigator.cs index a6f51a32bb..a81ef9e011 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigator.cs @@ -49,6 +49,7 @@ protected Navigator( { if (Logger.IsEnabled(LogLevel.Information)) Logger.LogInformationMessage($"Starting Navigation - Navigator: {this.GetType().Name} Request: {request.Route}"); request = request with { Source = this }; + return await Task.Run(() => NavigateAsync(request)); } diff --git a/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs b/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs index d74c288d7b..4d13c908c5 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs @@ -99,27 +99,28 @@ protected override async Task RegionCanNavigate(Route route, RouteInfo? ro return path; } - protected override Task PostNavigateAsync() + protected override async Task PostNavigateAsync() { if (Control is not null) { - foreach (var child in Control.Children.OfType()) + await Dispatcher.ExecuteAsync(async cancellation => { - if(child == CurrentlyVisibleControl) + foreach (var child in Control.Children.OfType()) { - child.Opacity = 1; - child.Visibility = Visibility.Visible; - } - else - { - child.Opacity = 0; - child.Visibility = Visibility.Collapsed; + if (child == CurrentlyVisibleControl) + { + child.Opacity = 1; + child.Visibility = Visibility.Visible; + } + else + { + child.Opacity = 0; + child.Visibility = Visibility.Collapsed; + } } - } + }); } - - return Task.CompletedTask; } private FrameworkElement? FindByPath(string? path) diff --git a/src/Uno.Extensions.Navigation/NavigationRequest.cs b/src/Uno.Extensions.Navigation/NavigationRequest.cs index 068de8814f..ace7967a9c 100644 --- a/src/Uno.Extensions.Navigation/NavigationRequest.cs +++ b/src/Uno.Extensions.Navigation/NavigationRequest.cs @@ -1,9 +1,11 @@ namespace Uno.Extensions.Navigation; #pragma warning disable SA1313 // Parameter names should begin with lower-case letter -public record NavigationRequest(object Sender, Route Route, CancellationToken? Cancellation = default, Type? Result = null, INavigator? Source = null) +public record NavigationRequest(object Sender, Route Route, CancellationToken? Cancellation = default, Type? Result = null) #pragma warning restore SA1313 // Parameter names should begin with lower-case letter { + internal INavigator? Source { get; init; } + public override string ToString() => $"Request [Sender: {Sender.GetType().Name}, Route:{Route}, Result: {Result?.Name ?? "N/A"}]"; internal virtual IResponseNavigator? GetResponseNavigator(IResponseNavigatorFactory responseFactory, INavigator navigator) => default;