uwp - 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"/>
解决方案
如果您的自定义类实现了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));
}
}
推荐阅读
- c - 如何使用 libimobiledevice 获取已连接设备的 ECID?
- laravel - 将响应数据重定向到另一个视图
- java - 如何用 ArrayList 替换 String[]?
- amazon-web-services - HTTPS 在使用 URL 时不重定向,但在键入 IP 时有效
- laravel - 将 laravel 分页链接更改为通过 HTTPS
- python-3.x - 启用急切执行时,从 keras 模型手动访问和更新可训练变量
- php - 找不到类'Maatwebsite\\Excel\\ExcelServiceProvider' - Laravel 4.2
- puppeteer - 如何 page.click 动态列表中具有给定值的元素
- javascript - 实时服务器和本地主机上的 Date().toLocaleString() 输出格式不同
- javascript - rxjs 传递 forkjoin 一个函数,该函数返回一个可观察的数组