Daniel
Daniel

Reputation: 2288

Why does the caliburn.Micro explanation not work?

I am working from the caliburn.micro website and they have this example. Why is password returning null?! See here the picture of the example I am trying to work with. enter image description here

My view.xaml:

<StackPanel>
    <TextBox x:Name="Username" />
    <PasswordBox x:Name="Password" />
    <Button x:Name="Login" Content="Log in" />
</StackPanel>

And in viewModel.cs:

public void Login(string username, string password){
    MessageBox.show(password + " " + username)
}

I have managed to get the login button to fire the Login method and it shows the username but blank for the password.

Upvotes: 1

Views: 609

Answers (2)

Nigel Sampson
Nigel Sampson

Reputation: 10609

The example works on some of the supported platforms, specifically Silverlight, Windows Phone and UWP.

On WPF however PasswordBox doesn't expose a dependency property for the entered password the framework can't bind the method input to the control.

One approach would be to create your own attached property that exposes the entered password from the control.

You can then customize the PasswordBox convention to use your new property.

Now that newer versions of Caliburn.Micro will be dropping support for some of these platforms it may be time to revisit the example on the homepage.

Upvotes: 1

Nkosi
Nkosi

Reputation: 247018

Consider using an alternative format which properties instead of parameters in the ViewModel

public class LoginViewModel : PropertyChangedBase {

    string username;
    public string Username {
        get { return username; }
        set { 
            username = value;
            NotifyOfPropertyChange(() => Username);
            NotifyOfPropertyChange(() => CanLogin);
        }
    }

    string password;
    public string Password {
        get { return password; }
        set { 
            password = value;
            NotifyOfPropertyChange(() => Password);
            NotifyOfPropertyChange(() => CanLogin);
        }
    }

    public bool CanLogin() {
        return !String.IsNullOrEmpty(Username) && !String.IsNullOrEmpty(Password);
    }

    public void Login() {
        MessageBox.show(Password + " " + Username)
    }
}

Upvotes: 2

Related Questions