这是我的 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);
    }

这是我的 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);
    }
}

当我尝试传递'用户'时,属性为null,甚至来自UI(View)的用户输入数据,如果我使用上述代码设置 UserSettings 都可以使用,但是我想简化一下,我怎样才能做到这一点?

谢谢

最佳答案

您想编写await loginService.SaveUserSettings(this);,因为this似乎是填充数据的目标。

关于c# - 如何在没有手动设置的情况下将绑定(bind)属性作为参数传递,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58371026/

10-11 15:59