Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injected INavigator is null for classes not linked to View #2608

Open
KWodarczyk opened this issue Nov 8, 2024 · 6 comments
Open

Injected INavigator is null for classes not linked to View #2608

KWodarczyk opened this issue Nov 8, 2024 · 6 comments
Labels
kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification.

Comments

@KWodarczyk
Copy link

Current behavior

I have SomeUtils class that takes
UnoAppNavigation2.zip
in INavigator as ctor parameter, at creation the parameter is null

image

App.xaml.cs

...omitted for brevity 
                .ConfigureServices((context, services) =>
                {
                    // TODO: Register your services
                    //services.AddSingleton<IMyService, MyService>();

                    services.AddSingleton<ISomeUtils, SomeUtils>();
                })
                .UseNavigation(RegisterRoutes)
            );
        MainWindow = builder.Window;

#if DEBUG
        MainWindow.EnableHotReload();
#endif
        MainWindow.SetWindowIcon();

        Host = builder.Build();


        await builder.NavigateAsync<Shell>();
    }

    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)
                ]
            )
        );
    }
}

MainPage.xaml cs getting ISomeUtils class instance

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

        var somUtils = _app.Host.Services.GetRequiredService<ISomeUtils>();

        INavigator navigator = this.Navigator();

        this.InitializeComponent();

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

SomeUtils class

public class SomeUtils : ISomeUtils
{
    private INavigator _navigation;

    public SomeUtils(INavigator navigator)
    {
        _navigation = navigator;
    }

    public int MyProperty { get; set; }
}

Expected behavior

INavigator should not be null when injected to a SomeUtils class

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

Build and run project attached, put a brakepoint in SomeUtlis class ctor. INavigator parameter will be null.

Workaround

No response

Works on UWP/WinUI

None

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

No response

Affected platforms

Windows (WinAppSDK)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

@KWodarczyk KWodarczyk added kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification. labels Nov 8, 2024
@jeromelaban jeromelaban transferred this issue from unoplatform/uno Nov 11, 2024
@lhLife
Copy link

lhLife commented Nov 18, 2024

modify to:
services.AddTransient<ISomeUtils, SomeUtils>();


@KWodarczyk
Copy link
Author

@lhLife .AddTransient does fix the issue but, it's not ideal as it forces developer to use Transient, even if they want a Singleton, I would treat this as temporary work around. I also think that this should be documented in Uno docs for example somewhere on this page https://platform.uno/docs/articles/external/uno.extensions/doc/Reference/Navigation/Navigator.html

@lhLife
Copy link

lhLife commented Nov 19, 2024

It cannot be a singleton, it can only expose INavigatorFactory or INavigator wrapper。
The entire program is a tree like structure that can be navigated anywhere。
demo:Not real code
Where do you think INavigator should navigate from

<Page>
    <Frame Navigator="FrameNavigator">
        <Page2>
            <Panel1 Navigator="PanelVisiblityNavigator">
                <Page3/>
            </Panel1>
            <Panel2 Navigator="PanelVisiblityNavigator">
                <Page4/>
            </Panel2>
        </Page2>
    </Frame>
</Page>

@lhLife
Copy link

lhLife commented Nov 19, 2024

The setting of the region is good, but INavigator needs a wrapper class to solve its complexity.

@KWodarczyk
Copy link
Author

It cannot be a singleton, it can only expose INavigatorFactory or INavigator wrapper。
The entire program is a tree like structure that can be navigated anywhere。
demo:Not real code
Where do you think INavigator should navigate from

<Page>
    <Frame Navigator="FrameNavigator">
        <Page2>
            <Panel1 Navigator="PanelVisiblityNavigator">
                <Page3/>
            </Panel1>
            <Panel2 Navigator="PanelVisiblityNavigator">
                <Page4/>
            </Panel2>
        </Page2>
    </Frame>
</Page>

Sorry I don't follow, you have two regions with same name ?

@lhLife
Copy link

lhLife commented Nov 21, 2024

Each page is a separate page.
When you navigate through the Model on Page 4, there are PanelVisilityNavigator and FrameNavigator at the top level, and multiple options will appear. Therefore, they can only be Transient (PanelVisilityNavigator)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification.
Projects
None yet
Development

No branches or pull requests

2 participants