How to get Uno DI and Prism DI to work together. #3356
Replies: 3 comments 3 replies
-
|
So I'm not sure how you've setup your code but Prism for Uno.WinUI specifically takes a dependency on |
Beta Was this translation helpful? Give feedback.
-
|
Then there's something very wrong going up here because there's definitively crashing, lemme prepare the smallest possible piece of code to copy it out |
Beta Was this translation helpful? Give feedback.
-
|
This is the exception (or part of it, it's huge): My App.xaml.cs public partial class App : PrismApplication
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
InitializeComponent();
}
/// <inheritdoc />
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register ShellViewModel
containerRegistry.Register<ShellViewModel>();
// Don't register Uno Extensions services here - they'll be registered in ConfigureServices
// Register application services directly in Prism container
containerRegistry.RegisterSingleton<IColorThemeService, ColorThemeService>();
containerRegistry.RegisterSingleton<ITokenService, TokenService>();
containerRegistry.RegisterSingleton<IJwtService, JwtService>();
containerRegistry.RegisterSingleton<IAuthenticationService, AuthService>();
containerRegistry.RegisterSingleton<FlagCache>();
////// REST OMMITTED FOR BREVITY
// Register ViewModels
containerRegistry.Register<NewsViewModel>();
containerRegistry.Register<MainViewModel>();
// Register views for navigation
containerRegistry.RegisterForNavigation<MainPage, MainViewModel>();
////// REST OMMITTED FOR BREVITY
}
/// <inheritdoc />
protected override UIElement CreateShell() => Container.Resolve<Shell>();
protected override void ConfigureHost(IHostBuilder builder)
{
builder
#if DEBUG
// Switch to Development environment when running in DEBUG
.UseEnvironment(Environments.Development)
#endif
.UseLogging((context, logBuilder) =>
{
// Configure log levels for different categories of logging
logBuilder.SetMinimumLevel(context.HostingEnvironment.IsDevelopment()
? LogLevel.Information
: LogLevel.Warning)
// Default filters for core Uno Platform namespaces
.CoreLogLevel(LogLevel.Warning);
// Uno Platform namespace filter groups
// Uncomment individual methods to see more detailed logging
////// REST OMMITTED FOR BREVITY
},
true)
.UseSerilog(true, true)
.UseConfiguration(configure: configBuilder => configBuilder.EmbeddedSource<App>().Section<AppConfig>())
// Enable localization (see appsettings.json for supported languages)
.UseLocalization()
.UseHttp((context, services) =>
{
services.AddTransient<DelegatingHandler, HttpAuthHandler>();
#if DEBUG
// DelegatingHandler will be automatically injected
services.AddTransient<DelegatingHandler, DebugHttpHandler>();
#endif
services.AddKiotaClientV2<ApiClient>(context,
new EndpointOptions
{
Url = context.Configuration.GetSection("ApiClient:Url")
.Value ??
// Fallback to a default URL if not configured
"https://localhost:5023"
});
});
}
protected override void ConfigureWindow(Window window)
{
#if DEBUG
window.UseStudio();
#endif
window.SetWindowIcon();
}
}Shell.xaml: <UserControl x:Class="Marechai.App.Presentation.Views.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:utu="using:Uno.Toolkit.UI"
xmlns:prism="using:Prism.Navigation.Regions"
xmlns:local="using:Marechai.App.Presentation.Views"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!-- Content region for Prism navigation -->
<ContentControl prism:RegionManager.RegionName="ContentRegion"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" />
</UserControl>Shell.xaml.cs using System;
using Marechai.App.Presentation.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Prism.Navigation.Regions;
namespace Marechai.App.Presentation.Views;
public sealed partial class Shell : UserControl
{
public Shell(ShellViewModel viewModel, IRegionManager regionManager)
{
InitializeComponent();
DataContext = viewModel;
// Set the region manager on this control to ensure regions are scanned
RegionManager.SetRegionManager(this, regionManager);
// Navigate after Shell is loaded and regions are registered
Loaded += (s, e) =>
{
Console.WriteLine("Shell.Loaded event fired");
viewModel.OnShellLoaded();
};
}
}MainViewModel.cs declaration and constructor: public partial class MainViewModel : ObservableObject, IRegionAware
public MainViewModel(IRegionManager regionManager, ITokenService tokenService, IJwtService jwtService,
IAuthenticationService authService
//, NewsViewModel newsViewModel, IColorThemeService colorThemeService, IThemeService themeService
)
{
Console.WriteLine("MainViewModel constructor called");
_regionManager = regionManager;
_tokenService = tokenService;
_jwtService = jwtService;
_authService = authService;
//NewsViewModel = newsViewModel;
// Initialize color theme service with theme service
//_ = InitializeThemeServicesAsync(colorThemeService, themeService);
// Initialize commands
NavigateToNewsCommand = new AsyncRelayCommand(NavigateToMainAsync);
NavigateToBooksCommand = new AsyncRelayCommand(() => NavigateTo("books"));
NavigateToCompaniesCommand = new AsyncRelayCommand(() => NavigateTo("companies"));
NavigateToComputersCommand = new AsyncRelayCommand(() => NavigateTo("computers"));
NavigateToConsolesCommand = new AsyncRelayCommand(() => NavigateTo("consoles"));
NavigateToDocumentsCommand = new AsyncRelayCommand(() => NavigateTo("documents"));
NavigateToDumpsCommand = new AsyncRelayCommand(() => NavigateTo("dumps"));
NavigateToGraphicalProcessingUnitsCommand = new AsyncRelayCommand(() => NavigateTo("gpus"));
NavigateToMagazinesCommand = new AsyncRelayCommand(() => NavigateTo("magazines"));
NavigateToPeopleCommand = new AsyncRelayCommand(() => NavigateTo("people"));
NavigateToProcessorsCommand = new AsyncRelayCommand(() => NavigateTo("processors"));
NavigateToSoftwareCommand = new AsyncRelayCommand(() => NavigateTo("software"));
NavigateToSoundSynthesizersCommand = new AsyncRelayCommand(() => NavigateTo("sound-synths"));
NavigateToUsersCommand = new AsyncRelayCommand(() => NavigateTo("users"));
NavigateToSettingsCommand = new AsyncRelayCommand(() => NavigateTo("settings"));
LoginLogoutCommand = new RelayCommand(HandleLoginLogout);
ToggleSidebarCommand = new RelayCommand(() => IsSidebarOpen = !IsSidebarOpen);
// Subscribe to authentication events
_authService.LoggedOut += OnLoggedOut;
}AuthService declaration and constructor: public sealed class AuthService : IAuthenticationService
{
private readonly ITokenService _tokenService;
private ApiClient? _apiClient;
private IStringLocalizer? _localizer;
public AuthService(ApiClient apiClient, ITokenService tokenService, IStringLocalizer localizer)
{
_tokenService = tokenService;
_apiClient = apiClient;
_localizer = localizer;
}I've been all day trying to solve this with all kind of workarounds to no avail I don't understand what's going on any help would be so much appreciated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I have services added in the ConfigureHost or ConfigureApplication builder, like for example the StringLocalizer or the Kiota client, how can I make them accessible to the Prism dependency injector so they can be injected into the view models.
These are not easily replaceable services indeed...
And I cannot access Host in the RegisterTypes to extract them from Uno's DI and add to the Prism Container.
Beta Was this translation helpful? Give feedback.
All reactions