Search code examples
mvvmcross

problem navigating from app to baseview model with this base ctor


I have a base class on baseviewmodel.

I'm facing navigationservice implemented on 6.2 on debug shows problem navigating to another viewmodel.

debug shows userdialogs break.

Is there a problem using base class in this way with those parameters . anyone faced this kind of issue

        public BaseViewModel(IMvxNavigationService navigationService, 
                             ILoginService loginService,
                             UserDialogs userDialogs, IValidator validator) {
            _navigationService = navigationService;
            _loginService = loginService;
            _userDialogs = userDialogs;
            _validator = validator;
            Title = TextSource.GetText(StringResourceKeys.Title);
            IsBusyMessage = Resources.Strings.LoadingMesssage;
        }

using gettext provider like this

public class ResourcexTextProvider : IMvxTextProvider { private readonly ResourceManager _resourceManager;

    public ResourcexTextProvider(ResourceManager resourceManager)
    {
        _resourceManager = resourceManager;
        CurrentLanguage = CultureInfo.CurrentUICulture;
    }

    public CultureInfo CurrentLanguage { get; set; }

    public string GetText(string namespaceKey, string typeKey, string name)
    {
        string resolvedKey = name;

        if (!string.IsNullOrEmpty(typeKey))
        {
            resolvedKey = $"{typeKey}.{resolvedKey}";
        }

        if (!string.IsNullOrEmpty(namespaceKey))
        {
            resolvedKey = $"{namespaceKey}.{resolvedKey}";
        }

        return _resourceManager.GetString(resolvedKey, CurrentLanguage);
    }

    public string GetText(string namespaceKey, string typeKey, string name, params object[] formatArgs)
    {
        string baseText = GetText(namespaceKey, typeKey, name);

        if (string.IsNullOrEmpty(baseText))
        {
            return baseText;
        }

        return string.Format(baseText, formatArgs);
    }

    public bool TryGetText(out string textValue, string namespaceKey, string typeKey, string name)
    {
        throw new System.NotImplementedException();
    }

    public bool TryGetText(out string textValue, string namespaceKey, string typeKey, string name, params object[] formatArgs)
    {
        throw new System.NotImplementedException();
    }
}

}


Solution

  • You're trying to inject UserDialogs userDialogs in the ctor of your BaseViewModel. My guess is that you missed registering the userDialogs.

    First of all you should inject interfaces instead of implementations to improve maintainability:

    Mvx.IocConstruct.RegisterType<IUserDialogs, UserDialogs>();
    

    And if my guess is correct and you are using Acr.UserDialogs you should initialize it and register it as:

    Mvx.IoCProvider.RegisterSingleton<IUserDialogs>(() => UserDialogs.Instance);
    

    Then you can inject it in any ViewModel directly using the interface:

    public BaseViewModel(IMvxNavigationService navigationService, 
                         ILoginService loginService,
                         IUserDialogs userDialogs, 
                         IValidator validator) {
            _navigationService = navigationService;
            _loginService = loginService;
            _userDialogs = userDialogs;
            _validator = validator;
            Title = TextSource.GetText(StringResourceKeys.Title);
            IsBusyMessage = Resources.Strings.LoadingMesssage;
        }
    

    HIH