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] NullReferenceException when calling `.Region(r => r.Attached(true))` #2040

Open johnnyelwailer opened 1 year ago

johnnyelwailer commented 1 year ago

Good day I am using C# markup and trying to implement a basic NavigationView.

Current behavior

I got a basic NavigationView displayed, but clicking on an item would not lanch a navigation.

I then went over the docs (for which there is unfortunately no Version for C# Markup): https://platform.uno/docs/articles/external/uno.extensions/doc/Learn/Tutorials/Navigation/Advanced/HowTo-UseNavigationView.html

It says something like add the uen:Region.Attached everywhere, I don't understand why, but I assumed it is important. So I found the equivalent in C#: .Region(r => r.Attached(true))

Since I added that to any of the elements in the Shell, I am receiving a Null Reference Exception: Logger needs to be set

Expected behavior

There should be no error, and navigation should work

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

Create an app from the template activating the routing using regions, and csharp markup.

public sealed partial class Shell : UserControl, IContentControlProvider
{
    public Shell()
    {
        this.Content(
            new Border()
                .Child(
                    new NavigationView()
                        .Region(r => r.Attached(true))
                        .PaneDisplayMode(NavigationViewPaneDisplayMode.LeftMinimal)
                        .MenuItems(
                            new NavigationViewItem()
                                .Content("Home")
                                .Icon(new SymbolIcon(Symbol.Home))
                                .Region(r => r.Name("Main"))
                         .Content(
                            new ExtendedSplashScreen()
                             .Region(r => r.Attached(true))
                             .Assign(out var view)
                             .HorizontalContentAlignment(HorizontalAlignment.Stretch)
                             .VerticalContentAlignment(VerticalAlignment.Stretch)
                             .LoadingContentTemplate<object>(_ => new Grid()
                                .RowDefinitions(new GridLength(2, GridUnitType.Star), new GridLength(1, GridUnitType.Star))
                                .Children(
                                    new ProgressRing()
                                        .Grid(row: 1)
                                        .VerticalAlignment(VerticalAlignment.Center)
                                        .HorizontalAlignment(HorizontalAlignment.Center)
                                        .Height(100)
                                        .Width(100)
                                )
                            )
                         )
                    )
                .HorizontalAlignment(HorizontalAlignment.Stretch)
                .VerticalAlignment(VerticalAlignment.Stretch)
            )
            .Background(Theme.Brushes.Background.Default);

        ContentControl = view;
    }

    public ContentControl ContentControl { get; }
}

Environment

Winui, .Net 8

Package Version(s): UNO.WinUi 5.0.41

Affected platform(s):

Visual Studio:

Relevant plugins:

Anything else we need to know?

I saw there was already a bufix related to this exception here: https://github.com/unoplatform/uno.extensions/issues/1732 Unfortunately, it doesn't seem to apply for me

dansiegel commented 11 months ago

@nickrandolph this appears to be a bug in Extensions.Navigation

Exception: System.NullReferenceException
Message: Logger needs to be set
Source: Uno.Extensions.Navigation.UI
StackTrace:
   at Uno.Extensions.Navigation.UI.Region.get_Logger()
   at Uno.Extensions.Navigation.UI.Region.AttachedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at ABI.Microsoft.UI.Xaml.PropertyChangedCallback.Do_Abi_Invoke(IntPtr thisPtr, IntPtr d, IntPtr e)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IDependencyObjectMethods.SetValue(IObjectReference _obj, DependencyProperty dp, Object value)
   at Microsoft.UI.Xaml.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Uno.Extensions.Navigation.UI.RegionPropertyBuilder.Attached(Boolean attached)
   at UnoApp3.Presentation.Shell.<>c.<.ctor>b__0_0(RegionPropertyBuilder r) in Y:\temp\UnoApp3\UnoApp3\UnoApp3\Presentation\Shell.cs:line 11

UnoApp3.zip

xperiandri commented 8 months ago

Any workaround?

xperiandri commented 8 months ago

4.1.0-dev.74 still have a bug

xperiandri commented 8 months ago

So I figured out what is wrong @johnnyelwailer see explanation in https://github.com/unoplatform/uno.extensions/issues/215