SirJohnK/LocalizationResourceManager.Maui

using _localizationResourceManager

Closed this issue · 7 comments

jrahma commented

How can I use the _localizationResourceManager without passing it as parameter to

public MainPage(ILocalizationResourceManager localizationResourceManager)

LocalizationResourceManager will be registered in DI container when UseLocalizationResourceManager is called. So you could resolve it with the ServiceProvider. But then you need a reference to that...

public MainPage(IServiceProvider serviceProvider)
{
  var resourceManager = serviceProvider.GetRequiredService<ILocalizationResourceManager>();
}

What is the problem with injecting ILocalizationResourceManager in your MainPage?

It will be automatically be resolved when navigating to it with Shell, if the page is registered in AppShell or page route is registered. If you don't use Shell you need to register AND resolve the page by your own in e.g. a NavigationService.

jrahma commented

but f do:

public MainPage(IServiceProvider serviceProvider)
in .NET MAUI app it will throw:

System.MissingMethodException: No parameterless constructor defined for type ‘MyApp.Mobile.MainPage'.
at at System.RuntimeType.CreateInstanceMono(Boolean nonPublic, Boolean wrapExceptions)
at at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at at System.Activator.CreateInstance(Type type)
at at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()
at at Microsoft.Maui.Controls.ElementTemplate.CreateContent()
at at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)
at at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()
at at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()
at at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()
at at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
at at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
at MyApp.Program.Main(String[] args) in /Users/jassim/Projects/MyApp/MyApp/Platforms/iOS/Program.cs:13

when I start my app using AppShell:

MainPage = new Mobile.AppShell();

Then the MainPage is not registered with DI and/or the navigation is not done with Shell.

Can you share your project or a sample project with the same layout/problem, so I can see the whole solution?

jrahma commented

Here is a sample

LocalizationSample.zip

For the DI injection to work, you need to register the MainPage in MauiProgram.CreateMauiApp.

Look at MauiProgram.cs in the sample project.

builder.Services.AddSingleton<MainPage>();

...or as Transient, depending on your needs...

builder.Services.AddTransient<MainPage>();

After that, you can use:

public MainPage(ILocalizationResourceManager resourceManager)

...or...

public MainPage(IServiceProvider serviceProvider)

Don't forget to add a Resources File and register that in MauiProgram.CreateMauiApp, like in the sample application.

.UseLocalizationResourceManager(settings =>
{
    settings.AddResource(AppResources.ResourceManager);
    settings.RestoreLatestCulture(true);
});
jrahma commented

Yes it's wroking.

I have one few more questions:

  1. Can I define the ILocalizationResourceManager and the xmlns:localization once and use it across my entire app?

  2. if I am going to do it with every page and I have HomePage which is a ContentView opened from MainPage like this:

Mobile.HomePage HomePageTabView = new Mobile.HomePage();

Then how can I pass the:

ILocalizationResourceManager resourceManager
Do I need to pass it with every page I am opening?

  1. I need to pass it with every page then how can I pass it wth this AppShell:

await Shell.Current.GoToAsync($"{nameof(MemberDetails)}?MemberID={(e.DataItem as HomePageData).member_id}");

  1. No, you need to inject ILocalizationResourceManager into ViewModels/Views where you need it and reference xmlns:localization on all XAML views where you need it.
  2. Read How to use in a ContentView in the Wiki how to solve that.
  3. If the page/view is registered in DI, ILocalizationResourceManager will be automatically injected into your page/view constructor, if added as a parameter.