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

Use from ViewModels not having a dependency on MAUI? #22

Open
hansmbakker opened this issue Jan 4, 2024 · 5 comments
Open

Use from ViewModels not having a dependency on MAUI? #22

hansmbakker opened this issue Jan 4, 2024 · 5 comments

Comments

@hansmbakker
Copy link

hansmbakker commented Jan 4, 2024

I have my solution split in several projects where my project containing a ViewModels does not have a dependency on MAUI - just on the MVVM Community Toolkit.

Is there a way to access the ILocalizationResourceManager from ViewModels without taking a dependency on MAUI via LocalizationResourceManager.Maui?

E.g. in this example app, the MAUI dependent parts of localization and the view-agnostic parts of localization are put in separate projects so that the ViewModels can reference just the interface.

I see I could create a kind of wrapper pair (interface + implementation) myself, but it would be great to have this in the library!

@hansmbakker
Copy link
Author

hansmbakker commented Aug 30, 2024

@SirJohnK would be curious to hear your thoughts on this! Am happy to create a PR that extracts the interfaces into a LocalizationManager.Core project, which is referenced by the LocalizationManager.Maui project if that helps (it will need a separate nuget package as well then).

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 2, 2024

@hansmbakker , I like the idea to extract the ILocalizationResourceManager interface to enable reference without the dependency on MAUI!

Solution:

  • New LocalizationResourceManager.Maui.Core NuGet.
  • For backwards compability, still use the LocalizationResourceManager.Maui namespace.

I will look into this tonight. 😄

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 4, 2024

@hansmbakker , I think I have found a really nice solution for this! 😄

So now you can do this ViewModel with only help from MVVM Community Toolkit:

using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using LocalizationResourceManager.Maui.Core;

namespace LocalizationResourceManager.Maui.Sample.Common;

public partial class MainViewModel : ObservableObject
{
    public LocalizedString HelloWorld { get; }
    public LocalizedString CurrentCulture { get; }

    public MainViewModel(ILocalizationResourceManager resourceManager)
    {
        //Init
        HelloWorld = resourceManager.CreateLocalizedString(() => $"{resourceManager["Hello"]}, {resourceManager["World"]}!");
        CurrentCulture = new(resourceManager, () => resourceManager.CurrentCulture.NativeName);
    }

    [ObservableProperty]
    private int count;

    [RelayCommand]
    private void CounterClicked() => Count++;
}

What do you think?

@hansmbakker
Copy link
Author

That looks good! Great!

@SirJohnK
Copy link
Owner

SirJohnK commented Sep 5, 2024

@hansmbakker, if you have the time, please test it out and report back your findings. Would be great to have some feedback before I will go live with this version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants