Hi all,
I’m working with the App UI package and trying to integrate navigation with dependency injection. My issue is that the NavHost requires parameterless constructors when navigating to a screen. However, I want to inject the ViewModel into my screen’s constructor, similar to how it’s done in the MVVM example (where the MainPage takes the MainViewModel as constructor dependency).
Is there a way to navigate while still using DI to inject dependencies into the screen’s ViewModel?
Thanks!
In case somebody has the same question… I decided to use the Microsoft.Extensions.DependencyInjection package to register and resolve dependency as described in the Community Toolkit documentation here.
It looks like in recent versions of the package there’s a NavHost.makeScreen property where you can set your own delegate to instantiate NavigationScreen objects.
https://docs.unity3d.com/Packages/com.unity.dt.app-ui@2.0/api/Unity.AppUI.Navigation.NavHost.html#Unity_AppUI_Navigation_NavHost_makeScreen
1 Like
I see that they also introduced App.current.services, which returns the current IServiceProvider.
Exactly what I have needed 
2 Likes
Another question came up regarding Navigation:
NavDestinations contain also a “ShowNavigationRail” flag. Looking into the source, I realized that in contrast to the other flags (like “ShowDrawer” etc.), this flag is currently not used at all.
Are there any future plans to implement also a navigation rail (in my understanding, a permanently visible side navigation bar)?
Indeed, the NavigationRail hasn’t been a priority for now but I think it’s gonna be part of the 2.0.0 official release. And you are right our concept of NavigationRail is basically the one from Material Design.
1 Like
Hi Mickael, thanks for the fast response. Is there a rough timeline for the official release of 2.0.0?
No precise release date, but should happen before the end of this year.
1 Like
Hello @mickael-bonfill,
Once again, we have a question about navigation: It seems like currently, views are always re-created (constructor invoked) even when navigating back within a navigation stack. Is there any built-in way to keep the view instances while building up the stack? Or to restore otherwise when going back?
Hello!
Indeed, there is no built-in way to do that at the moment, it would make sense to keep in the StackView of the NavHost all views that are part of the same backstack (while maintaining sync with save/restore backstack state during navigation). That’s a precious feedback you brought here 
We should be able to add this for 2.0.0 or 2.1.0.
In the meantime, if you want to save and restore views yourself, you could cache them in a static Dictionary
(using the NavDestination name as a unique key) and try to find the view during the OnEnter callback. At this point, your NavigationScreen is an empty implementation that only stores and delivers views from a cache, but it is still a good workaround.
1 Like
When using appui-canvas to drag, there is no limit on scrollOffset, and you can drag all the way. How can I set boundaries for dragging? Setting MinZoom.MaxZoom only limits the zoom, not the dragging range