Error on guard clause with Caliburn.Micro

2019-06-04 07:26发布

问题:

I am trying to implement so guard handling with Caliburn.Micro but I am getting an invalid cast exception when the application runs.

The Property:

    public Account UserAccount
    {
        get
        {
            return account;
        }
        set
        {
                account = value;
                NotifyOfPropertyChange(() => UserAccount);
                NotifyOfPropertyChange(() => CanSaveAndNavigateToComposeView());
        }
    }

The Method:

    public void SaveAndNavigateToComposeView()
    {
        CommitAccountToStorage();
        navigationService.UriFor<ComposeViewModel>().Navigate();
    }

The Guard:

    public bool CanSaveAndNavigateToComposeView()
    {
        return !(string.IsNullOrEmpty(UserAccount.DisplayName) ||
                 string.IsNullOrEmpty(UserAccount.Username)    ||
                 string.IsNullOrEmpty(UserAccount.Password)    || 
                 string.IsNullOrEmpty(UserAccount.ServerSymbol));
    }

The guard works if I take out the notify prop change on the property but this means my method would never evaluate.

回答1:

You need to make CanSaveAndNavigateToComposeView into a property rather than a method.