使用此基本构造函数从应用程序导航到基本视图模型时出现问题

problem navigating from app to baseview model with this base ctor

我在 baseviewmodel 上有一个基础 class。

我在调试时遇到在 6.2 上实现的导航服务显示导航到另一个视图模型时出现问题。

调试显示用户对话框中断。

以这种方式使用基数 class 和这些参数是否有问题。任何人都遇到过这种问题

        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;
        }

像这样使用 gettext 提供程序

public class ResourcexTextProvider : IMvxTextProvider { 私人只读 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();
    }
}

}

您正在尝试将 UserDialogs userDialogs 注入 BaseViewModel 的构造函数中。我猜你错过了注册 userDialogs。

首先你应该注入接口而不是实现来提高可维护性:

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

如果我的猜测是正确的并且你正在使用 Acr.UserDialogs 你应该 initialize it and register it 为:

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

然后你可以直接使用接口将它注入任何ViewModel:

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;
    }