首页 > 解决方案 > PropertyChange 在 UWP 中总是返回 NULL?

问题描述

我正在为 Windows 10 制作 UWP 商店应用程序。我使用 API 并连接到服务器。

我只需要将文本框绑定到无效登录的错误消息。但是在调用 OnPropertyChanged eventHandler 方法时总是返回 null 值?

public LoginPasswordView()
{
    this.InitializeComponent();
    var vm = new LoginPasswordViewModel(new NavigationService(), new LoginService());
    this.DataContext = vm;      
}

视图模型.cs

public class LoginPasswordViewModel : MainViewBaseModel
{
    private string password;
    private string errorMessage="We need this info to countinue";

    public LoginPasswordViewModel(INavigationService navigationService, ILoginService loginService)
    {
        _navigationService = navigationService;
        _loginService = loginService;
    }

    private INavigationService _navigationService { get; set; }
    private ILoginService _loginService { get; set; }

    public string Errormessage
    {
        get
        {
            return errorMessage;
        }
        set
        {
            errorMessage = value;
            OnPropertyChanged("Errormessage");
        }
    }
}

public class MainViewBaseModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
            eventHandler(this, new PropertyChangedEventArgs(propertyName));           
    }
}

XAML:

<TextBlock Text="{Binding Path=Errormessage,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="250,25,0,0" FontSize="24" FontFamily="Segoe Pro" Foreground="Red"/>

标签: uwpinotifypropertychanged

解决方案


如果您的自定义类实现了INotifyPropertyChanged接口并调用OnPropertyChanged属性中的处理程序set,它将通知 UI(通常是绑定客户端)属性值已更改。

所以,您的问题实际上是您如何更改属性的值,但我没有看到您Errormessage在上面的代码中更改 ' 值的位置。

我根据你的代码做了一个简单的代码示例供你参考。

<StackPanel>
    <TextBox Text="{Binding Password,Mode=TwoWay}"></TextBox>
    <TextBlock Text="{Binding Path=Errormessage,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="250,25,0,0" FontSize="24" FontFamily="Segoe Pro" Foreground="Red"/>
</StackPanel>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        var vm = new LoginPasswordViewModel();
        this.DataContext = vm;
    }
}

public class LoginPasswordViewModel : MainViewBaseModel
{
    private string password;
    private string errorMessage = "We need this info to countinue";

    public LoginPasswordViewModel()
    {

    }

    public string Password
    {
        get { return password; }
        set
        {
            password = value;
            if (string.IsNullOrEmpty(Password.Trim()))
            {
                Errormessage = "Password cannot be null or empty!";
            }
            else
            {
                Errormessage = string.Empty;
            }
            OnPropertyChanged("Password");
        }
    }


    public string Errormessage
    {
        get
        {
            return errorMessage;
        }
        set
        {
            errorMessage = value;
            OnPropertyChanged("Errormessage");
        }
    }
}

public class MainViewBaseModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
}

推荐阅读