How to pass Bound properties as parameters without manual setup

Viewed 47

Here is my App.xaml.cs

protected override Task OnInitializeAsync(IActivatedEventArgs args) {
        Container.RegisterInstance(SessionStateService);
        Container.RegisterInstance(NavigationService);

        Container.RegisterType<IUserRepository, UserRespository>(new ContainerControlledLifetimeManager());
        Container.RegisterType<UserSettingsContext>(new ContainerControlledLifetimeManager());
        Container.RegisterType<IUserSettings, UserSettings>(new ContainerControlledLifetimeManager());

        var userSettings = this.Container.Resolve<UserSettingsContext>();
        userSettings.Database.EnsureCreated();

        return base.OnInitializeAsync(args);
    }

This is my UserSettings

using using Prism.Windows.Validation;

public class UserSettings : ValidatableBindableBase, IUserSettings {
    private string _firstName;
    public string FirstName {
        get { return _firstName; }
        set { SetProperty(ref _firstName, value); }
    }

    private string _lastName;
    public string LastName {
        get { return _lastName; }
        set { SetProperty(ref _lastName, value); }
    }

    private UserType _userType;
    public UserType UserType {
        get { return _userType; }
        set { SetProperty(ref _userType, value); }
    }

    private string _username;
    [Required(ErrorMessage = "Required.")]
    public string Username {
        get { return _username; }
        set { SetProperty(ref _username, value); }
    }

    private string _password;
    [Required(ErrorMessage = "Required.")]
    public string Password {
        get { return _password; }
        set { SetProperty(ref _password, value); }
    }
}

ViewModel:

public class TestViewModel : UserSettings {
    private UserSettings user;
    public UserSettings User => user;

    public async void SaveSettings() {
        //It will work with this line
        // var user = new UserSettings() {
            // Username= this.Username,
            // Password= this.Password,
            // UserType= this.UserType,
            // FirstName= this.FirstName,
            // LastName= this.LastName
        // };

        //null properties
        await loginService.SaveUserSettings(user);
    }
}

When I tried to pass the 'user' the properties are null ,even user input data from UI(View), It will work if I setup the UserSettings using the code above, but I want to simplify this without that, how can I do that?

Thanks

1 Answers

You want to write await loginService.SaveUserSettings(this); because this seems to be the target that's filled with data.

Related