【问题标题】:Bindable ObservableCollection not update Calendar UI可绑定的 ObservableCollection 不更新日历 UI
【发布时间】:2019-04-24 09:13:22
【问题描述】:

我在我的项目中使用this 日历。在日历视图中,我添加了两个按钮来选择周末日期。单击按钮时,“Findes”方法在可绑定的 ObservableCollection SelectedDates 中添加日期。如果我使用这种方法在列表中添加新的日期,只在更改月份时更新。通知 OnPropertyChanged 时用户界面如何更新?

视图模型

    public class CalendarioViewViewModel : MvxViewModel, INotifyPropertyChanged
{

    public CalendarioViewViewModel()
    {
        _dates = new ObservableCollection<DateTime>();
        _dates.Add(new DateTime().AddDays(5));

    }

    public DateTime Date
    {
        get =>  DateTime.Now;
    }

    private ObservableCollection<DateTime> _dates;

    public ObservableCollection<DateTime> Dates
    {
        get { return _dates; }
        set { 
            SetProperty(ref _dates, value);
        }
    }

    private Command _finSemanaCommand;
    public Command FinSemanaCommand =>
        _finSemanaCommand ?? (_finSemanaCommand = new Command(Finde));

    public void Finde()
    {
        Dates.Add(DateTime.Now.AddDays(7));
        OnPropertyChanged("Dates");


    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {

        var propertyChangedCallback = PropertyChanged;
        propertyChangedCallback?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

Xalm 视图

 <calendar:Calendar 
                x:Name="calendar"
                MultiSelectDates="True"
                SelectedBackgroundColor="Khaki"
                SelectedBorderWidth="0"
                BorderColor="Transparent"
                StartDay="Monday"
                EnableTitleMonthYearView="True"
                SelectRange="False"
                SelectedDates="{Binding Dates, Mode=TwoWay}"
                MinDate="{Binding Date}"
                TitleLabelFormat="MMM yyyy"
                HorizontalOptions="Center">

【问题讨论】:

  • 文件作为包作者的问题。我在代码中没有看到任何让我认为支持动态更新 SelectedDates 的内容

标签: xamarin.forms binding calendar observablecollection


【解决方案1】:

我找到了解决问题的“解决方案”:

CalendarioView.Xaml:

<Button x:Name="finde_btn" Text="Finde" BackgroundColor="White" BorderWidth="3" BorderColor="#2DABE2" CornerRadius="10" Grid.Row="2" Clicked="Button_Clicked_Finde"
                    FontSize="13" />

<calendar:Calendar 
                x:Name="calendar"
                MultiSelectDates="True"
                SelectedBorderWidth="0"
                BorderColor="Transparent"
                StartDay="Monday"
                EnableTitleMonthYearView="True"
                SelectRange="False"
                SelectedDates="{Binding Dates, Mode=TwoWay}"
                MinDate="{Binding Date}"
                TitleLabelFormat="MMM yyyy"
                HorizontalOptions="Center">

CalendarioView.xaml.cs

public partial class CalendarioView : MvxContentPage<CalendarioViewModel>
{
    public CalendarioView ()
    {
        InitializeComponent ();
    }


    public void Button_Clicked_Finde(object sender, System.EventArgs e)
    {
        ViewModel.Finde(calendar.StartDate);
        calendar.RaiseSpecialDatesChanged();
    }
........

CalendarioViewModel.cs

  public class CalendarioViewModel : MvxViewModel, INotifyPropertyChanged
{

    public CalendarioViewModel()
    {
        _dates = new ObservableCollection<DateTime>();

    }

    public DateTime Date
    {
        get =>  DateTime.Now;
    }

    private ObservableCollection<DateTime> _dates;

    public ObservableCollection<DateTime> Dates
    {
        get { return _dates; }
        set { 
            SetProperty(ref _dates, value);
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {

        var propertyChangedCallback = PropertyChanged;
        propertyChangedCallback?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

     public void Finde(DateTime startDate)
    {
        List<DateTime> fechas = GetWeekend(startDate);

        foreach (DateTime dateTime in fechas)
        {
            Dates.Add(dateTime);

        }


        OnPropertyChanged("Dates");


    }
........

在简历中:

按钮点击->在Calendario.xalm.cs中调用Button_Clicked_Finde->在ViewModel中调用Finde函数->完成后执行calendar.RaiseSpecialDatesChanged()并更新UI日历

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2012-02-29
    • 2011-10-25
    相关资源
    最近更新 更多