unoplatform / uno.extensions

Libraries to ease common developer tasks associated with building multi-platform mobile, desktop and web applications using Uno Platform or WinAppSDK.
https://platform.uno/
Other
75 stars 47 forks source link

Navigation RouteNotifier.RouteChanged not invoked #2606

Open KWodarczyk opened 2 weeks ago

KWodarczyk commented 2 weeks ago

Current behavior

RouteNotifier.RouteChanged is not fired, Application it stuck at loading screen

Expected behavior

RouteNotifier.RouteChanged should fire when navigator.NavigateRouteAsync is called

App.xaml.cs

    private void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
    {
        views.Register(
            new ViewMap(ViewModel: typeof(ShellViewModel)),
            new ViewMap<MainPage, MainViewModel>(),
            new ViewMap<FirstPage, FirstViewModel>(),
            new DataViewMap<SecondPage, SecondViewModel, Entity>()
        );

        routes.Register(
            new RouteMap("", View: views.FindByViewModel<ShellViewModel>(),
                Nested:
                [
                    new ("Main", View: views.FindByViewModel<MainViewModel>(),IsDefault:true)
                ]
            )
        );
    }

Main Page xaml

<Page x:Class="UnoAppNavigation2.Presentation.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:UnoAppNavigation2"
      xmlns:Presentation="using:UnoAppNavigation2.Presentation"
      xmlns:uen="using:Uno.Extensions.Navigation.UI"
      xmlns:utu="using:Uno.Toolkit.UI"
      Loaded="Page_Loaded"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

  <Grid uen:Region.Attached="True" RowDefinitions="auto,*">
    <StackPanel Grid.Row="0" HorizontalAlignment="Left" Orientation="Horizontal" Background="DarkSlateGray">
         <TextBlock>
           <Run Text="URL:"/>
           <Run x:Name="UrlPath"/>
         </TextBlock>
    </StackPanel>

        <NavigationView Grid.Row="1"
            uen:Region.Attached="true"
            PaneDisplayMode="Top"
            IsSettingsVisible="False"
            ItemInvoked="NavigationView_ItemInvoked"
            IsBackButtonVisible="Collapsed"
            IsBackEnabled="False">
            <NavigationView.MenuItems>

                <NavigationViewItem
                  Content="One"
                  x:Name="OneTab"
                  uen:Region.Name="{x:Bind local:PageNames.FirstPage}" />

                <NavigationViewItem
                  Content="Two"
                  x:Name="TwoTab"
                  uen:Region.Name="{x:Bind local:PageNames.SecondPage}" />

            </NavigationView.MenuItems>

            <Grid uen:Region.Attached="True"
                  uen:Region.Navigator="Visibility">

              <Presentation:FirstPage Visibility="Collapsed" uen:Region.Name="{x:Bind local:PageNames.FirstPage}"/>
              <Presentation:SecondPage Visibility="Collapsed" uen:Region.Name="{x:Bind local:PageNames.SecondPage}"/>

            </Grid>
        </NavigationView>
    </Grid>
</Page>

Main page that navigates to FirstPage on Loaded

public sealed partial class MainPage : Page
{
    private App? _app;

    public MainViewModel? ViewModel { get; set; }

    public MainPage()
    {
        _app = (Application.Current as App);

        INavigator navigator = this.Navigator();

        this.InitializeComponent();

        UrlPath.Text =  _app?.MainHref + " base url:"+_app.BaseUrl;
    }

    private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
    {
        Console.WriteLine($"==== Navigation invoked =====");
    }

    private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
        var navigator = this.Navigator();
        Console.WriteLine($"====    Navigation to {PageNames.FirstPage} =======");
        var result = await navigator.NavigateRouteAsync(this, PageNames.FirstPage);  //This does not invoke RouteChange
    }
}

First Page that subscribes to RoutChanged event

public sealed partial class FirstPage : Page
{
    private App _app;

    public FirstPage()
    {
        _app = (Application.Current as App);

        this.InitializeComponent();

        var notifier = _app.Host.Services.GetService<IRouteNotifier>();

        notifier.RouteChanged += OnRouteChanged;

        Debug.WriteLine("============= First  Page Ctor ===========");

        Debug.WriteLine($"is Rount NOtifier null ? {notifier is null}");
    }

    private void OnRouteChanged(object? sender, RouteChangedEventArgs e)
    {
        Debug.WriteLine($"===== In First Page route changed to {e.Region.Name}");
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        Console.WriteLine("============= First  Page Loaded ===========");
    }

    private void Page_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
    {
        Console.WriteLine("============= First  Page Context Canchged ===========");
    }
}

Page Loaded screen

image

Application gets stuck at loading

image

How to reproduce it (as minimally and precisely as possible)

Build project attached and run it in Windows Apps Sdk target or WASM [Uploading UnoAppNavigation2.zip…]()

Environment

Nuget Package (s):

Package Version(s):

"Uno.Sdk": "5.4.10"

Affected platform(s):

Visual Studio:

Relevant plugins:

Visual Studio 2022 "Uno.Sdk": "5.4.10"

Anything else we need to know?

eriklimakc commented 1 week ago

@KWodarczyk Thank you for reporting this issue. It seems that the link to the repro app you provided is not working correctly. Could you please check and provide an updated link? Thank you!

KWodarczyk commented 1 week ago

@eriklimakc hi, not sure if i still have that project but will try to find it, i used "click to add files" and added .zip but not sure why it's not working. I will try to do it again but this can be also reproduces from the pasted code above.

KWodarczyk commented 1 week ago

UnoAppNavigation2.zip

@eriklimakc hope this works