一键执行两个中继命令-WPF

时间:2018-08-23 18:42:48

标签: c# wpf mvvm user-controls visibility

我有一个按钮,允许用户注册帐户。当用户按下按钮时,我有一个中继命令,执行一种将其详细信息提交到数据库的方法。如果成功,那么我希望将用户转移到登录屏幕。

当我使用触发可见或隐藏UserControl的转换器时,我使用了EnumStateVisibility。

这是我拥有的代码:

在按下时将帐户详细信息提交到数据库

Submit account details button

/// <summary>
        /// Submits users details to database and checks criteria
        /// </summary>
        public RelayCommand SubmitRegisterAccountDetails
        {
            get
            {
                SubmitAccountDetails submit = new SubmitAccountDetails();
                return new RelayCommand(param => submit.SubmitLoginUserDetails(EmailAddress, Mail, Password, CurrentState));
            }
        }




// Enum State to visibility converter, toggles login and register views on and off in window
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                LoginVisibilityStates state = (LoginVisibilityStates)value;
                LoginVisibilityStates para = (LoginVisibilityStates)Enum.Parse(typeof(LoginVisibilityStates), parameter.ToString());

                if (state == para)
                    return Visibility.Visible;
                else
                    return Visibility.Hidden;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }



// Logic to switch UserControl when user presses a different button, with
// this relaycommand set as the command and "Login" or "Register" as the CommandParameter

    private LoginVisibilityStates _currentState;
        public LoginVisibilityStates CurrentState
        {
            get
            {
                return _currentState;
            }
            set
            {
                _currentState = value;
                OnPropertyChanged("CurrentState");
            }
        }

        #region Commands
        public RelayCommand SwitchCommand
        {
            get
            {
                return new RelayCommand(ChangeState);
            }
        }
        #endregion

        public void ChangeState(object parameter)
        {
            switch (parameter.ToString())
            {
                case "Register":
                    CurrentState = LoginVisibilityStates.Register;
                    break;
                case "Login":
                    CurrentState = LoginVisibilityStates.Login;
                    break;
                default:
                    break;
            }
        }

1 个答案:

答案 0 :(得分:0)

您只能执行一个命令,但是在此命令的Execute方法中,您可以执行所需的任何操作,包括调用另一命令,设置属性或调用方法:

return new RelayCommand(param => 
{
    submit.SubmitLoginUserDetails(EmailAddress, Mail, Password, CurrentState);
    //set the property or do whatever here:
    LoginVisibilityStates = LoginVisibilityStates.Login;
});