编辑:从评论中看,我显然应该使用一个消息传递中心,如果无论如何它可以帮助我为我的项目实现该功能,我对于xamarin.forms还是有点陌生的。我不确定在阅读文档后是否理解它的工作方式。 / p>
当前,我在主页上创建TasksGroups,这些TasksGroups具有日期。 当我创建那些TasksGroup时,使用TasksGroups创建的日期的背景会更改颜色。
我正在使用:https://github.com/lubiepomaranczki/XamForms.Controls.Calendar
在此控件中,正是SpecialDates更改了日期的背景色。
我的问题:一旦我创建了一个新的TasksGroup(因此创建了一个新日期),它就不会自动更改日历上TasksGroupDate的背景颜色。更改将在我重新启动应用程序时发生。
在我的CalendarViewModel中,initialize()方法是我设置背景色的地方。
架构:TasksGroupPage是我的主页,是TabbedPage,CalendarPage是ContentPage。
有什么主意吗?感谢您的帮助!
让您了解它的外观:
主页:
日历页面:
CalendarPage.xaml,我将BindingContext设置为CalendarPageViewModel
<ContentView>
<controls:Calendar
Grid.Row="1"
Padding="10,0,10,0"
SelectedBorderWidth="4"
DisabledBorderColor="Black"
ShowNumberOfWeek="false"
StartDay="Monday"
TitleLabelTextColor="#008A00"
TitleLeftArrowTextColor="#008A00"
TitleRightArrowTextColor="#008A00"
SelectedDate="{Binding Date}"
SpecialDates="{Binding Attendances}"
DateCommand="{Binding DateChosen}"
DateClicked="DateClicked">
</controls:Calendar>
</ContentView>
CalendarPageViewModel.cs我在Initialize()方法中创建所有应该具有背景的日期
class CalendarPageViewModel : BaseViewModel
{
public CalendarPageViewModel()
{
Initialize();
}
private void Initialize()
{
var taskGroupList = App.Database.GetTaskGroupsAsync().GetAwaiter().GetResult();
Attendances = new ObservableCollection<SpecialDate>()
{
new SpecialDate(DateTime.Now)
{
BackgroundColor = Color.Green,
TextColor = Color.Accent,
Selectable = true
},
};
taskGroupList.ForEach(x =>
{
var sp = new SpecialDate(x.TasksGroupDate)
{
BackgroundColor = Color.Blue,
TextColor = Color.Accent,
Selectable = true
};
Attendances.Add(sp);
});
NotifyPropertyChanged();
}
}
** BaseViewModel.cs **
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<SpecialDate> attendances;
public ObservableCollection<SpecialDate> Attendances
{
get { return attendances; }
set
{
attendances = value;
NotifyPropertyChanged();
}
}
//other properties
}
我如何在NewtaskPageViewModel中保存我的TasksGroup
async Task SaveNewTask()
{
IsBusy = true;
await Task.Delay(4000);
IsBusy = false;
TasksGroup tasksGroup = new TasksGroup();
Tasks tasks = new Tasks();
tasksGroup.TasksGroupDescription = TasksGroupDescription;
tasksGroup.TasksGroupDate = TasksGroupDate;
tasks.TaskDuration = TaskDuration;
tasks.TaskDBA = TaskDBA;
tasks.TaskDescription = TaskDescription;
tasksGroup.Taches = new List<Tasks>() { tasks };
if (ValidateTasksGroup(tasksGroup) && ValidateTasks(tasks))
{
await App.Database.SaveTasksGroupAsync(tasksGroup);
await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
await Application.Current.MainPage.Navigation.PopAsync();
NotifyPropertyChanged();
}
}
答案 0 :(得分:0)
如果我理解正确,则可以使用SaveNewTask
方法发送邮件:
if (ValidateTasksGroup(tasksGroup) && ValidateTasks(tasks))
{
await App.Database.SaveTasksGroupAsync(tasksGroup);
//Send updateDate message
MessagingCenter.Send<Object, TasksGroup>(this, "updateDate", tasksGroup);
await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
await Application.Current.MainPage.Navigation.PopAsync();
NotifyPropertyChanged();
}
然后在您的CalendarPage
,subscribe
中显示消息并在那里更新数据
public partial class CalendarPage : ContentPage
{
CalendarPageViewModel CalendarPageVM { get; set; }
public CalendarPage()
{
InitializeComponent();
CalendarPageVM = new CalendarPageViewModel();
this.BindingContext = CalendarPageVM;
MessagingCenter.Subscribe<Object, TasksGroup>(new object(), "updateDate", async (sender, arg) =>
{
await DisplayAlert("Message received", "arg=" + arg, "OK");
//Update date here , arg here is the new tasksGroup you created in the SaveNewTask method
TasksGroup tasks = arg as TasksGroup;
var sp = new SpecialDate(tasks)
{
BackgroundColor = Color.Blue,
TextColor = Color.Accent,
Selectable = true
};
CalendarPageVM.Attendances.Add(sp);
});
}
}
答案 1 :(得分:0)
我终于解决了我的回答,我只是在CalendarPage.xaml.cs的OnAppearing方法中调用了Initialize方法
protected override void OnAppearing()
{
base.OnAppearing()
var vm = this.BindingContext as CalendarPageViewModel;
vm.Initialize();
}