PropertyChange在UWP中总是返回NULL吗?

时间:2018-09-06 09:54:30

标签: uwp inotifypropertychanged

我正在制作适用于Windows 10的UWP存储应用程序。我正在使用API​​,并且正在连接到服务器。

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

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

viewmodel.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"/>

2 个答案:

答案 0 :(得分:0)

使用 x:Bind 而不是 Binding 语法,x:Bind更加灵活和高效,而且出错的机会也更少。

https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension

,因为您使用的是 INotifyPropertyChanged ,所以您应将 Mode =“ OneWay” x:Bind 结合使用,详细信息请参见链接以上提供。

答案 1 :(得分:0)

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

因此,您的问题实际上是如何更改属性的值,但是在上面的代码中我没有看到您在何处更改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));
    }
}