【发布时间】:2021-05-31 09:44:21
【问题描述】:
我制作了一个 Xamarin 应用程序,它允许我在 DatePicker 中选择一个日期并使用 SQLite 将其保存到数据库中。然后我得到这些日期并在ListView 中显示它们,我想知道是否可以将数据库中的日期突出显示到日历上?
这是用来保存数据的
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");
}
然后将其加载到 ListView 中
private async void NavigateButton_OnClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page1());
}
protected override void OnAppearing()
{
base.OnAppearing();
using(SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<Birthdays>();
var birthday = conn.Table<Birthdays>().ToList();
birthdayListView.ItemsSource = birthday;
}
}
这是我用来加载日历和主页上的ListView 的 XML 即时通讯。
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" BackgroundColor="{AppThemeBinding Dark=Black, Light=White}" xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" mc:Ignorable="d" x:Class="Mobile_App.MainPage">
<ContentPage.ToolbarItems>
<ToolbarItem
Text="Settings"
Clicked="ToolbarItem_Clicked"
></ToolbarItem>
</ContentPage.ToolbarItems>
<StackLayout>
<!-- Place new controls here -->
<controls:Calendar DatesBackgroundColor="{AppThemeBinding Dark=Black, Light=White}" DatesTextColor="{AppThemeBinding Dark=White, Light=Black}" BackgroundColor="{AppThemeBinding Dark=Black, Light=White}" Padding="10,0,10,0" SelectedBorderWidth="4" DisabledBorderColor="Black" ShowNumberOfWeek="false" StartDay="Sunday" TitleLabelTextColor="Purple" TitleLeftArrowTextColor="MediumVioletRed" TitleRightArrowTextColor="MediumVioletRed" SpecialDates="{Binding Date}" DateCommand="{Binding DateChosen}" />
<Label Text="Welcome to the Birthday App!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Button Text="Add a birthday" AnchorX="1" AnchorY="1" Clicked="NavigateButton_OnClicked" />
<Switch AnchorX="1" AnchorY="1" IsToggled="False" Toggled="Switch_Toggled" />
<ListView HeightRequest="250" WidthRequest="50" BackgroundColor="{AppThemeBinding Dark=Black, Light=#444343}" x:Name="birthdayListView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding FullName}"
Detail="{Binding Date}"
DetailColor="YellowGreen">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
【问题讨论】:
-
Xamarin Forms 中没有标准的日历控件。您需要告诉我们(并请提供链接)您正在使用哪个日历控件。
-
我正在使用 XamForms.Controls.Calendar。
-
如果您查看示例项目,有一个 SpecialDates 属性,您可以将其绑定到日期列表以在日历上突出显示
-
我明白,我只是不知道如何处理我的数据库。还有如何改变日历本身的颜色。
-
从您的数据库中获取列表,使用 SpecialDate 类创建一个列表,并将其绑定到日历。我现在无法给你一个更完整的例子