C# 突出显示XamForms日历中的日期
我制作了一个Xamarin应用程序,允许我在C# 突出显示XamForms日历中的日期,c#,xaml,xamarin,C#,Xaml,Xamarin,我制作了一个Xamarin应用程序,允许我在日期选择器中选择一个日期,并使用SQLite将其保存到数据库中。然后我获取这些日期并在列表视图中显示它们,我想知道是否可以在日历上突出显示数据库中的日期 这是用来保存数据的 void SaveButton_OnClicked(object sender, EventArgs e) { Birthdays brithday = new Birthdays() { FirstName
日期选择器中选择一个日期,并使用SQLite将其保存到数据库中。然后我获取这些日期并在列表视图中显示它们,我想知道是否可以在日历上突出显示数据库中的日期
这是用来保存数据的
void SaveButton_OnClicked(object sender, EventArgs e)
{
Birthdays brithday = new Birthdays()
{
FirstName = fNameEntry.Text,
LastName = lNameEntry.Text,
Date = datePicker.Date
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<Birthdays>();
int rowsAdded = conn.Insert(brithday);
}
DisplayAlert("Alert", "Birthday has been saved to database", "OK");
}
正如Jason的回答,您可以从Sqlite数据库获取列表数据,然后创建observedcollection attents
以添加当前日期数据
表示动态数据集合,该集合在添加、删除项或刷新整个列表时提供通知
我做了一件简单的事情,你可以看看:
<StackLayout>
<controls:Calendar
Padding="10,0,10,0"
BackgroundColor="{AppThemeBinding Dark=Black,
Light=White}"
DateCommand="{Binding DateChosen}"
DatesBackgroundColor="{AppThemeBinding Dark=Black,
Light=White}"
DatesTextColor="{AppThemeBinding Dark=White,
Light=Black}"
DisabledBorderColor="Black"
SelectedBorderWidth="4"
SelectedDate="{Binding Date}"
ShowNumberOfWeek="false"
SpecialDates="{Binding attendances}"
StartDay="Sunday"
TitleLabelTextColor="Purple"
TitleLeftArrowTextColor="MediumVioletRed"
TitleRightArrowTextColor="MediumVioletRed" />
<ListView
x:Name="birthdayListView"
BackgroundColor="{AppThemeBinding Dark=Black,
Light=#444343}"
HeightRequest="250"
ItemsSource="{Binding birthdays}"
WidthRequest="50">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Detail="{Binding Date}"
DetailColor="YellowGreen"
Text="{Binding FullName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page38 : ContentPage, INotifyPropertyChanged
{
private List<Birthday> _birthdays;
public List<Birthday> birthdays
{
get { return _birthdays; }
set
{
_birthdays = value;
RaisePropertyChanged("birthdays");
}
}
private DateTime? _date;
public DateTime? Date
{
get
{
return _date;
}
set
{
_date = value;
RaisePropertyChanged(nameof(Date));
}
}
public ObservableCollection<XamForms.Controls.SpecialDate> attendances { get; set; }
public ICommand DateChosen
{
get
{
return new Command((obj) => {
System.Diagnostics.Debug.WriteLine(obj as DateTime?);
});
}
}
public Page38()
{
InitializeComponent();
attendances = new ObservableCollection<SpecialDate>();
Date = DateTime.Now;
this.BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
//get data from sqlite database.
birthdays = new List<Birthday>()
{
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,25), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,26), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,27), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,28), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,30), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,31), },
};
//foreach birthdays data, add Date property to attendances
foreach (Birthday b in birthdays)
{
attendances.Add(new XamForms.Controls.SpecialDate(b.Date) { BackgroundColor = Color.Green, TextColor = Color.White, BorderColor = Color.Yellow, BorderWidth = 8, Selectable = true });
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共分部类Page38:ContentPage,INotifyPropertyChanged
{
私人名单(生日);;
公开生日名单
{
获取{return\u birthdays;}
设置
{
_生日=价值;
RaisePropertyChanged(“生日”);
}
}
私人日期时间?\u日期;
公共日期时间?日期
{
得到
{
返回日期;
}
设置
{
_日期=价值;
RaiseProperty变更(名称(日期));
}
}
公众可观察的集合出席人数{get;set;}
公共ICommand日期选择
{
得到
{
返回新命令((obj)=>{
System.Diagnostics.Debug.WriteLine(obj作为日期时间?);
});
}
}
公共页38()
{
初始化组件();
出席人数=新观察到的集合();
Date=DateTime.Now;
this.BindingContext=this;
}
出现时受保护的覆盖无效()
{
base.OnAppearing();
//从sqlite数据库获取数据。
生日=新列表()
{
新生日({FirstName=“cherry”,Date=newdatetime(2021,5,25),},
新的生日({FirstName=“cherry”,Date=newdatetime(2021,5,26),},
新的生日({FirstName=“cherry”,Date=newdatetime(2021,5,27),},
新的生日({FirstName=“cherry”,Date=newdatetime(2021,5,28),},
新生日({FirstName=“cherry”,Date=newdatetime(2021,5,30),},
新生日({FirstName=“cherry”,Date=newdatetime(2021,5,31),},
};
//对于每个生日数据,将Date属性添加到Attentions
foreach(生日中的生日b)
{
Add(新的XamForms.Controls.SpecialDate(b.Date){BackgroundColor=Color.Green,TextColor=Color.White,BorderColor=Color.Yellow,BorderWidth=8,selective=true});
}
}
公共事件属性更改事件处理程序属性更改;
public void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
您需要实现INotifyPropertyChanged
,以通知已更改的数据
截图:
Xamarin表单中没有标准日历控件。您需要告诉我们(并请提供链接)您正在使用哪个日历控件。我正在使用XamForms.Controls.calendar。如果您查看示例项目,可以将SpecialDates属性绑定到日期列表,以便在日历上突出显示。我明白,我只是不知道如何使用我的数据库。以及如何更改日历本身的颜色。从数据库中获取列表,使用SpecialDate类创建列表,并将其绑定到日历。我现在不能给你一个更完整的例子。谢谢你的这个樱桃,这对我有很大帮助。:)请看一下这个,告诉我把这些放在哪里。我有点搞不清楚事情该怎么办。谢谢:D@JesseWebb我无法运行您的项目,您可以根据您的情况在包含控件:日历的ContentPage中修改我的代码。无需担心,谢谢您的帮助。
<StackLayout>
<controls:Calendar
Padding="10,0,10,0"
BackgroundColor="{AppThemeBinding Dark=Black,
Light=White}"
DateCommand="{Binding DateChosen}"
DatesBackgroundColor="{AppThemeBinding Dark=Black,
Light=White}"
DatesTextColor="{AppThemeBinding Dark=White,
Light=Black}"
DisabledBorderColor="Black"
SelectedBorderWidth="4"
SelectedDate="{Binding Date}"
ShowNumberOfWeek="false"
SpecialDates="{Binding attendances}"
StartDay="Sunday"
TitleLabelTextColor="Purple"
TitleLeftArrowTextColor="MediumVioletRed"
TitleRightArrowTextColor="MediumVioletRed" />
<ListView
x:Name="birthdayListView"
BackgroundColor="{AppThemeBinding Dark=Black,
Light=#444343}"
HeightRequest="250"
ItemsSource="{Binding birthdays}"
WidthRequest="50">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Detail="{Binding Date}"
DetailColor="YellowGreen"
Text="{Binding FullName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page38 : ContentPage, INotifyPropertyChanged
{
private List<Birthday> _birthdays;
public List<Birthday> birthdays
{
get { return _birthdays; }
set
{
_birthdays = value;
RaisePropertyChanged("birthdays");
}
}
private DateTime? _date;
public DateTime? Date
{
get
{
return _date;
}
set
{
_date = value;
RaisePropertyChanged(nameof(Date));
}
}
public ObservableCollection<XamForms.Controls.SpecialDate> attendances { get; set; }
public ICommand DateChosen
{
get
{
return new Command((obj) => {
System.Diagnostics.Debug.WriteLine(obj as DateTime?);
});
}
}
public Page38()
{
InitializeComponent();
attendances = new ObservableCollection<SpecialDate>();
Date = DateTime.Now;
this.BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
//get data from sqlite database.
birthdays = new List<Birthday>()
{
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,25), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,26), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,27), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,28), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,30), },
new Birthday(){FirstName="cherry", Date=new DateTime(2021,5,31), },
};
//foreach birthdays data, add Date property to attendances
foreach (Birthday b in birthdays)
{
attendances.Add(new XamForms.Controls.SpecialDate(b.Date) { BackgroundColor = Color.Green, TextColor = Color.White, BorderColor = Color.Yellow, BorderWidth = 8, Selectable = true });
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}