静态构造函数以某种方式阻止实例构造函数

时间:2016-09-10 12:09:43

标签: c# xamarin constructor

我有Xamarin.Forms应用程序。目前我在Android上测试我的应用程序,现在我遇到了下一个视图(页面)没有显示的问题。我开始调试它,当我导航到我的页面然后实例构造函数调用自身时,我停止了这里,在这个构造函数的主体中,我有另一个具有静态字段和静态方法的类的初始化。当这个静态构造函数初始化我的字段然后在它之后它跳过实例构造函数,因为它我认为它没有完成初始化所有的东西,但是在没有实际导航的情况下完成操作。 这是开始:

private void OnCreateNewUserButtonTapped()
    {
        _navigationService.NavigateAsync($"{nameof(NavigationBarPage)}/{nameof(LoginingPage)}");
    }

然后在这里:

public LoginingPageViewModel(INavigationService navigationService, IKeyboardHelper keyboardHelper, ISecuredDataProvider securedDataProvider)
    {
        _navigationService = navigationService;
        _keyboardHelper = keyboardHelper;
        _checkBox = new ShowPasswordCheckBoxModel();
        _entries = new LogInPageEntriesModel();

        _accountManager = new AccountManager(new ClientAuthorization(), securedDataProvider);

        Func<bool> isLogInCommandEnable = () =>
            StringService.CheckForNullOrEmpty(_entries.LoginText, _entries.PasswordText);

        CheckedCommand = new DelegateCommand(OnCheckBoxTapped);
        LogInCommand = new DelegateCommand(OnLogInTapped, isLogInCommandEnable);
        //_keyboardHelper.ShowKeyboard();
    }

这里是帐户管理器类,它工作不正确:

private readonly ClientAuthorization _clientAuthorization;
    private readonly ISecuredDataProvider _securedDataProvider;
    private static string _lastUser = GetLastUserFromStorage();
    private readonly List<string> _users;

    public AccountManager(ClientAuthorization clientAuthorization, ISecuredDataProvider securedDataProvider)
    {
        _clientAuthorization = clientAuthorization;
        _securedDataProvider = securedDataProvider;
        _users = _securedDataProvider.RetreiveAll(ConstantsService.ProviderName).Select(acc => acc.Username) as List<string>;
    }
...
 public static string GetLastUserFromStorage()
    {

        if ( Application.Current.Properties.ContainsKey("_lastUser") )
            return Application.Current.Properties[_lastUser] as string;

        return string.Empty;
    }

如果没有初始化“_lastUser”就启动它,那么它就可以完美。

1 个答案:

答案 0 :(得分:0)

您想在拉出值时将_lastUser放在引号中。

if ( Application.Current.Properties.ContainsKey("_lastUser") )
    return Application.Current.Properties["_lastUser"] as string;

如果没有它,你会发现堆栈溢出我会说。