Android 在Xamarin中的登录按钮上实现MVVM

Android 在Xamarin中的登录按钮上实现MVVM,android,xamarin,mvvm,Android,Xamarin,Mvvm,我有一个关于MVVM模式的问题。 我有一个带按钮的页面。我为按钮创建了一个LoginCommand,并创建了一个ViewModel来处理该命令。现在,如果LoginCommand成功,我希望该按钮更改为注销按钮 我刚刚开始使用MVVM,所以我不知道如何开始处理这个问题 谢谢, 雅各布所以我想你想要一个“登录”和“注销”按钮 <Button Content="{Binding ButtonContent}" Command="{Binding ClickCommand}"/> 要使其

我有一个关于MVVM模式的问题。 我有一个带按钮的页面。我为按钮创建了一个LoginCommand,并创建了一个ViewModel来处理该命令。现在,如果LoginCommand成功,我希望该按钮更改为注销按钮

我刚刚开始使用MVVM,所以我不知道如何开始处理这个问题

谢谢,
雅各布

所以我想你想要一个“登录”和“注销”按钮

<Button Content="{Binding ButtonContent}" Command="{Binding ClickCommand}"/>

要使其工作,您需要实现,以便viewmodel知道某些属性已更改

取决于您的按钮布局。如果它们是两个不同的按钮,则需要交替可见性。如果只是文本,只需更改文本属性即可

下面是通过更改文本在一个按钮上实现登录/注销的简单示例:

XAML

C视图模型

private bool isLoggedIn=false; 私有ICommand on ButtonCommand; 公共ICommand OnButtonClickCommand=>OnButtonClickCommand??onButtonClickCommand=新命令OnButtonClick; 私有字符串buttonText=Login; 公共字符串按钮文本 { get=>buttonText; 设置 { buttonText=值; 不动产变更; } } 私有无效按钮单击 { if!isLoggedIn&&Login { isLoggedIn=真; ButtonText=注销; } 其他的 { 注销; isLoggedIn=false; ButtonText=登录; } }
您还可以在按钮上使用DataTrigger。 根据已验证的属性,按钮的文本属性可以是登录或注销

Xaml代码:

private string _ButtonContent;
public string ButtonContent
{
    get { return _ButtonContent;?? (_ButtonContent = "Login"); }
    set
    { 
        _ButtonContent = value;
        NotifyPropertyChanged("ButtonContent"); 
    }
 }

private ICommand _ClickCommand;
public ICommand ClickCommand
{
    get { return _ClickCommand ?? (_ClickCommand = _LoginCommand); }
    set
    {
        _ClickCommand = value;
        NotifyPropertyChanged("ClickCommand");
    }
 } 

private ICommand _LoginCommand = new RelayCommand(f => Login());
private ICommand _LogoutCommand = new RelayCommand(f => Logout());

private void Login()
{
    // Do your Login stuff here
    // Create if statement for when to Login is not completed succesfully
    // switch the button   
    ButtonText = "Logout";
    ClickCommand = LoginCommand;
}

private void Logout()
{
    // Do your Logout stuff here

    // switch the button   
    ButtonText = "Login";
    ClickCommand = LogoutCommand;
}