【问题标题】:PropertyChange always return NULL in UWP?PropertyChange 在 UWP 中总是返回 NULL?
【发布时间】:2019-02-11 13:27:25
【问题描述】:

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

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

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

【问题讨论】:

  • 请在 XAML 中显示您的绑定表达式。
  • 您好,在向问题添加信息时,请对其进行编辑并添加。这可确保用户无需通读 cmets 即可全面了解您面临的问题。
  • 欢迎来到 Stack Overflow!其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更易于阅读/理解。请查看我的更改以确保它们反映您的意图。如果您对我有其他问题或反馈,请随时给我留言。
  • @GhostCat 谢谢。这就是我的意思

标签: uwp inotifypropertychanged


【解决方案1】:

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

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

并且因为您使用的是 INotifyPropertyChanged,您应该使用 Mode="OneWay"x:Bind 更多详细信息可以在链接中看到上面提供。

【讨论】:

    【解决方案2】:

    如果您的自定义类实现了INotifyPropertyChanged 接口并在属性的set 中调用OnPropertyChanged 处理程序,它将通知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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-16
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      相关资源
      最近更新 更多