【问题标题】:Changes in DataSource not being reflected in UIDataSource 中的更改未反映在 UI 中
【发布时间】:2019-05-06 19:58:39
【问题描述】:

我正在做一个简单的 xamarin 表单应用程序,其中有一个 TimeAttendance 模型、一个列表视图页面和一个时间输入页面。当我从列表中选择一个时间条目时,它会将我带到添加/编辑页面,我可以在其中更改所有下拉/条目/时间选择器值并正确保存。

我遇到的问题是我正在根据开始时间和结束时间计算总小时数。当我关闭一天时,此值会正确保存,但即使设置了绑定属性,它也不会在 UI 中更新。我可以在后端代码中看到该值已更新,但它不会反映在 UI 中,除非我关闭一天并再次打开它。

看起来从源到目标的绑定只在第一次加载时触发,但我一直在阅读,这些视图被默认设置为双向绑定。

xml代码:

<Label Text="Total Hours"/>
<Label x:Name="totalHours"
   Text="{Binding TotalHours}"/>

<Button Text="CLOSE" Clicked="CloseDay_Clicked"/>
<Button Text="DELETE" Clicked="Delete_Clicked"/>

后面的代码:

async void CloseDay_Clicked(object sender, EventArgs e)
    {
        var timeEntry = (TimeAttendance)BindingContext;
        timeEntry.idUser = 1;
        await App.Database.SaveTimeAttendanceAsync(timeEntry);
        //await Navigation.PopAsync();
    }

    async void TimePicker_timeChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == TimePicker.TimeProperty.PropertyName)
        {
            var timeEntry = (TimeAttendance)BindingContext;
            var totalHours = timeEntry.EndTime - timeEntry.StartTime;
            timeEntry.TotalHours = totalHours.Hours + (totalHours.Minutes / 15) * 0.25;

        }
    }

bindingContext 是从列表页面设置的:

    async void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        await Navigation.PushAsync(new TimeAttendanceEntryPage
        {
            BindingContext = e.SelectedItem as TimeAttendance,
        });
    }

我的考勤模式:

public class TimeAttendance
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public int idUser { get; set; }
    public DateTime Date { get; set; } = DateTime.Today;
    public string Commodity { get; set; }
    public string Exception { get; set; }
    public string RequestType { get; set; }
    public string RequestId { get; set; }
    public TimeSpan StartTime { get; set; }
    public TimeSpan EndTime { get; set; }
    public string Activity { get; set; }
    public double TotalHours { get; set; }

}

正如我所说,代码在更新关闭和保存值时工作正常,我只是不知道为什么当我更新 TotalHours 时它会正确保存,但我看不到直接绑定到此值的标签中的更改.

【问题讨论】:

    标签: xamarin data-binding


    【解决方案1】:

    你只需要改变你的 Timeattendance 模型,让它像这样实现 INotifyPropertyChanged 接口:

    public class TimeAttendance : INotifyPropertyChanged
      {
        ...
       double totalHours;
       public double TotalHours {
            set
            {
                if (totalHours != value)
                {
                    totalHours = value;
                    OnPropertyChanged("TotalHours");
    
                }
            }
            get
            {
                return totalHours;
            }
       }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
      }
    

    【讨论】:

    • 效果很好!感谢分享。我对如何使用 INotifyPropertyChanged 进行了更多研究,我发现这个docs.microsoft.com/en-us/dotnet/api/… 看起来像你的答案。谢谢你的时间。
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    相关资源
    最近更新 更多