首页 文章

使用此基础ctor从app导航到baseview模型的问题

提问于
浏览
0

我在 baseviewmodel 上有一个基类 .

我正面临在6.2上实现的导航服务,调试显示导航到另一个视图模型的问题 .

debug显示userdialogs break .

以这种方式使用基类与这些参数是否存在问题 . 任何人都遇到过这种问题

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 {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();
    }
}

}

1 回答

  • 1

    你试图在 BaseViewModel 的ctor中注入 UserDialogs userDialogs . 我的猜测是你错过了注册userDialogs .

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

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

    如果我的猜测是正确的你正在使用 Acr.UserDialogs 你应该initialize itregister 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;
        }
    

    HIH

相关问题