UWP x:与时间绑定

本文关键字:时间 绑定 UWP | 更新日期: 2023-09-27 18:13:57

我将我的应用程序从WP 8.1/w8.1转换为UWP。它包括一个更新文本框值的计时器。下面是XAML:

Text="{Binding CurrentLocalDateTime, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}"

和数据上下文:

    private DateTime currentLocalDateTime;
    public DateTime CurrentLocalDateTime { get { return currentLocalDateTime; } set { currentLocalDateTime = value; OnPropertyChanged("CurrentLocalDateTime"); } }
    private void TimerExecution(object sender, object e)
    {
        CurrentLocalDateTime = DateTime.Now;
    }

我想使用新的x:Bind绑定方式,但控件从未更新,使用以下代码:

Text="{x:Bind CurrentLocalDateTime, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}"

数据上下文:

    public DateTime CurrentLocalDateTime { get; set; }
    private void TimerExecution(object sender, object e)
    {
        CurrentLocalDateTime = DateTime.Now;
    }

怎么了?

非常感谢你的帮助。关于

UWP x:与时间绑定

当你设置CurrentLocalDateTime时,没有什么可以通知你的UI它发生了。它在第一种情况下工作,因为你正在实现INotifyPropertyChanged和调用OnPropertyChanged与属性名称。

public DateTime CurrentLocalDateTime 
{ 
    get { return currentLocalDateTime; }  
    set 
    { 
        currentLocalDateTime = value;
        OnPropertyChanged("CurrentLocalDateTime"); 
    } 
}