In this article we will be discuss how to add Birthday and Anniversary reminders in Windows Phone 7 and how to store the Birthday and Anniversary Date and Time in the Local database. For this use the following procedure.
Steps
Step 1
First we write the following code in "AddOccasion.xaml":
<Grid x:Name="ContentPanel" Margin="12,1,12,-1" Grid.Row="1">
<Grid.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/wooden-background.jpg" />
</Grid.Background>
<TextBlock FontFamily="Portable User Interface" FontSize="22" Height="30" HorizontalAlignment="Left" Margin="12,83,0,0" Name="txtBlkBirthDay" Text="Birth Day" VerticalAlignment="Top" FontWeight="Bold" />
<TextBlock FontFamily="Portable User Interface" FontSize="22" Height="30" HorizontalAlignment="Left" Margin="9,158,0,0" Name="txtBlkAnniversary" Text="Anniversary" VerticalAlignment="Top" FontWeight="Bold" />
<toolkit:DatePicker HorizontalAlignment="Left" Margin="169,62,0,0" Name="BirthDaydatePicker" VerticalAlignment="Top" Width="248" />
<toolkit:DatePicker HorizontalAlignment="Left" Margin="169,139,0,0" Name="AnniversarydatePicker" VerticalAlignment="Top" Width="248" />
<Button Content="Add BirthDay Reminder" Height="72" HorizontalAlignment="Left" Margin="6,344,0,0" Name="btnAddBirthDayReminder" VerticalAlignment="Top" Width="208" FontSize="14" Click="btnAddBirthDayReminder_Click" FontWeight="Normal">
<Button.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</Button.Background>
</Button>
<Button Content="Add Occasions" FontSize="18" Height="72" HorizontalAlignment="Left" Margin="18,252,0,0" Name="btnAddOccasions" VerticalAlignment="Top" Width="184" Click="btnAddOccasions_Click" FontWeight="Normal">
<Button.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</Button.Background>
</Button>
<ListBox Height="35" HorizontalAlignment="Right" Margin="0,439,17,0" Name="lstBirthday" VerticalAlignment="Top" Width="309" Tap="lstBirthday_Tap">
<ListBox.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</ListBox.Background>
<ListBox.BorderBrush>
<ImageBrush ImageSource="/Contact;component/Image/Dark%20Wooden%20Background.jpg" />
</ListBox.BorderBrush>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Background>
<ImageBrush ImageSource="/Contact;component/Image/Dark%20Wooden%20Background.jpg" />
</StackPanel.Background>
<TextBlock Text="{Binding Path=BOccasion}"></TextBlock>
<TextBlock Width="30" Height="30" />
<TextBlock Text="{Binding Path=BirthDayBeginDate}"></TextBlock>
<TextBlock Width="30" Height="30" />
<TextBlock Text="{Binding Path=BirthDayBeginTime}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Add Anniversary Reminder" FontSize="14" Height="72" HorizontalAlignment="Left" Margin="220,344,0,0" Name="btnAddAnniversaryReminder" VerticalAlignment="Top" Width="230" Click="btnAddAnniversaryReminder_Click" FontWeight="Normal">
<Button.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</Button.Background>
</Button>
<ListBox Height="35" HorizontalAlignment="Left" Margin="130,480,0,0" Name="lstAnniversary" VerticalAlignment="Top" Width="308" Tap="lstAnniversary_Tap" Foreground="White">
<ListBox.BorderBrush>
<ImageBrush ImageSource="/Contact;component/Image/Dark%20Wooden%20Background.jpg" />
</ListBox.BorderBrush>
<ListBox.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</ListBox.Background>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Background>
<ImageBrush ImageSource="/Contact;component/Image/Dark%20Wooden%20Background.jpg" />
</StackPanel.Background>
<TextBlock Text="{Binding Path=AOccasion}" />
<TextBlock Width="20" Height="20" />
<TextBlock Text="{Binding Path=AnniversaryBeginDate}" />
<TextBlock Width="20" Height="20" />
<TextBlock Text="{Binding Path=AnniversaryBeginTime}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock FontFamily="Portable User Interface" FontSize="22" FontWeight="Bold" Height="30" HorizontalAlignment="Left" Margin="6,439,0,0" Name="txblkbday" Text="Birth Day" VerticalAlignment="Top" Visibility="Collapsed" />
<TextBlock FontFamily="Portable User Interface" FontSize="22" FontWeight="Bold" Height="30" HorizontalAlignment="Left" Margin="0,485,0,0" Name="txblkAni" Text="Anniversary" VerticalAlignment="Top" Visibility="Collapsed" />
<TextBlock FontFamily="Portable User Interface" FontSize="22" FontWeight="Bold" Height="30" HorizontalAlignment="Left" Margin="10,20,0,0" Name="textBlock1" Text="Name" VerticalAlignment="Top" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="169,6,0,0" Name="txtName" Text="" VerticalAlignment="Top" Width="248" />
</Grid>
</Grid>
The output will be:
Here we use the DatePicker Control to get the date.
Step 2
Now we will write the code for storing the date in the database. For that we first create the class ("ContactInfo.cs") in which we create a table like this:
[Table]
public class ContactInfo
{
[Column(Storage = "Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public string Id
{
get;
set;
}
[Column(CanBeNull = true)]
public string Name
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthyDay
{
get;
set;
}
[Column(CanBeNull = true)]
public string Anniversary
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayTime
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryTime
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayBeginDate
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayBeginTime
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayExpirationDate
{
get;
set;
}
[Column(CanBeNull = true)]
public string BOccasion
{
get;
set;
}
[Column(CanBeNull = true)]
public string AOccasion
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayExpirationTime
{
get;
set;
}
[Column(CanBeNull = true)]
public string BirthDayContent
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryContent
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryBeginDate
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryBeginTime
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryExpirationDate
{
get;
set;
}
[Column(CanBeNull = true)]
public string AnniversaryExpirationTime
{
get;
set;
}
}
}
Then we create another class ("ContactDataContext.cs") like this:
public class ContactdataContext:DataContext
{
public ContactdataContext(string connectionString)
: base(connectionString)
{
}
public Table<ContactInfo> Contacts
{
get
{
return this.GetTable<ContactInfo>();
}
}
}
Step 3
Now we will write the code for the "Add Occasion" button.
First we create a database like this:
private const string strConnectionString = @"isostore:/ContactDB.sdf";
public string parameterValue="0";
public AddOccasion()
{
InitializeComponent();
using (ContactdataContext context = new ContactdataContext(strConnectionString))
{
if (!context.DatabaseExists())
{
context.CreateDatabase();
}
}
}
private void btnAddOccasions_Click(object sender, RoutedEventArgs e)
{
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
ContactInfo newContact = new ContactInfo
{
Name=txtName.Text.ToString(),
BirthyDay = Convert.ToString(BirthDaydatePicker.Value.Value.Date),
Anniversary=Convert.ToString(AnniversarydatePicker.Value.Value.Date)
};
ContactDB.Contacts.InsertOnSubmit(newContact);
ContactDB.SubmitChanges();
}
MessageBox.Show("Saved");
}
The output will be:
Step 4
Now we will write the code for the "Add BirthDay Reminder" button:
First we create another page ("NewPageForOccasionsReminder.xaml"):
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var a = from b in ContactDB.GetTable<ContactInfo>() select b;
foreach (var x in a)
{
parameterValue = x.Id;
}
}
if (Action == "EditData")
{
NavigationService.Navigate(new Uri(string.Format("/NewPageForOccasionsReminder.xaml?parameter={0}&action={1}&Occasion={2}", parameterValue.ToString(), "Edit", "BirthDay"), UriKind.Relative));
}
else
{
NavigationService.Navigate(new Uri(string.Format("/NewPageForOccasionsReminder.xaml?parameter={0}&action={1}&Occasion={2}", parameterValue.ToString(), "EditData", "BirthDay"), UriKind.Relative));
}
Now we send the Id of that specific person in the QueryString to the next page that will help store the data for the specified person.
Step 5
Now we will write the code for "NewPageForOccasionsReminder.xaml":
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/wooden-background.jpg" />
</Grid.Background>
<TextBlock Height="30" HorizontalAlignment="Left" Margin="26,94,0,0" Name="ttName" Text="Occasion" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="156,68,0,0" Name="txtOccasion" Text="" VerticalAlignment="Top" Width="291" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="26,172,0,0" Name="ttContent" Text="Content" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
<TextBox Height="105" HorizontalAlignment="Left" Margin="159,146,0,0" Name="txtContent" Text="" VerticalAlignment="Top" Width="274" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="26,327,0,0" Name="ttBegintime" Text="Begin Time" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
<Button Content="Add" Height="72" HorizontalAlignment="Right" Margin="0,508,13,0" Name="btnAdd" VerticalAlignment="Top" Width="160" Click="btnAdd_Click">
<Button.Background>
<ImageBrush ImageSource="/AddReminder;component/Image/Dark%20Wooden%20Background.jpg" />
</Button.Background>
</Button>
<toolkit:TimePicker HorizontalAlignment="Left" Margin="170,307,0,0" Name="timePicker1" VerticalAlignment="Top" Width="166" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="7,446,0,0" Name="textBlock1" Text="Expiration Time" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
<toolkit:TimePicker HorizontalAlignment="Left" Margin="176,0,0,111" Name="timePicker2" VerticalAlignment="Bottom" Width="166" />
<toolkit:DatePicker HorizontalAlignment="Left" Margin="161,249,0,0" Name="datePicker1" VerticalAlignment="Top" FontSize="18" Height="54" Width="172" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="28,268,0,0" Name="textBlock2" Text="Begin Date" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
<toolkit:DatePicker FontSize="18" Height="54" HorizontalAlignment="Left" Margin="168,370,0,0" Name="datePicker2" VerticalAlignment="Top" Width="172" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="6,387,0,0" Name="textBlock3" Text="Expiration Date" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Portable User Interface" FontSize="22" />
</Grid>
Step 6
Now we fetch the QueryString data Like this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
parameterValue = NavigationContext.QueryString["parameter"];
Action = NavigationContext.QueryString["action"];
occasion = NavigationContext.QueryString["Occasion"];
txtOccasion.Text = occasion.ToString();
}
Step 7
Now we will write the code for "Add the Reminder Data" like this:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (occasion == "BirthDay")
{
string alarmName = Guid.NewGuid().ToString();
// use guid for alarm name, since alarm names must be unique
Alarm alarm = new Alarm("first");
alarm.Content = txtContent.Text.ToString();
DateTime date = (DateTime)datePicker1.Value;
DateTime time = (DateTime)timePicker1.Value;
DateTime beginTime = date + time.TimeOfDay;
alarm.BeginTime = beginTime;
DateTime date1 = (DateTime)datePicker2.Value;
DateTime time1 = (DateTime)timePicker2.Value;
DateTime exptime = date1 + time1.TimeOfDay;
alarm.ExpirationTime = exptime;
alarm.RecurrenceType = RecurrenceInterval.None;
ScheduledActionService.Add(alarm);
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var contact = (from i in ContactDB.GetTable<ContactInfo>()
where i.Id== parameterValue.ToString()
select i).Single();
contact.BOccasion = txtOccasion.Text.ToString();
DateTime bd1 = Convert.ToDateTime(datePicker1.Value);
DateTime bd2 = Convert.ToDateTime(datePicker1.Value);
DateTime bt1 = Convert.ToDateTime(timePicker1.Value);
DateTime bt2 = Convert.ToDateTime(timePicker1.Value);
contact.BirthDayContent = txtContent.Text.ToString();
contact.BirthDayBeginDate = Convert.ToString(bd1.ToShortDateString());
contact.BirthDayBeginTime = Convert.ToString(bt1.ToShortTimeString());
contact.BirthDayExpirationDate = Convert.ToString(bd2.ToShortDateString());
contact.BirthDayExpirationTime = Convert.ToString(bt1.ToShortTimeString());
ContactDB.SubmitChanges();
}
}
else
{
string alarmName = Guid.NewGuid().ToString();
// use guid for alarm name, since alarm names must be unique
Alarm alarm = new Alarm("second");
alarm.Content = txtContent.Text.ToString();
DateTime date = (DateTime)datePicker1.Value;
DateTime time = (DateTime)timePicker1.Value;
DateTime beginTime = date + time.TimeOfDay;
alarm.BeginTime = beginTime;
DateTime date1 = (DateTime)datePicker2.Value;
DateTime time1 = (DateTime)timePicker2.Value;
DateTime exptime = date1 + time1.TimeOfDay;
alarm.ExpirationTime = exptime;
alarm.RecurrenceType = RecurrenceInterval.None;
ScheduledActionService.Add(alarm);
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var contact = (from i in ContactDB.GetTable<ContactInfo>()
where i.Id == parameterValue.ToString()
select i).Single();
contact.AOccasion = txtOccasion.Text.ToString();
DateTime bd1 = Convert.ToDateTime(datePicker1.Value);
DateTime bd2 = Convert.ToDateTime(datePicker1.Value);
DateTime bt1 = Convert.ToDateTime(timePicker1.Value);
DateTime bt2 = Convert.ToDateTime(timePicker1.Value);
contact.AnniversaryContent = txtContent.Text.ToString();
contact.AnniversaryBeginDate = Convert.ToString(bd1.ToShortDateString());
contact.AnniversaryBeginTime = Convert.ToString(bt1.ToShortTimeString());
contact.AnniversaryExpirationDate = Convert.ToString(bd2.ToShortDateString());
contact.AnniversaryExpirationTime = Convert.ToString(bt1.ToShortTimeString());
ContactDB.SubmitChanges();
}
}
NavigationService.Navigate(new Uri(string.Format("/AddOccasion.xaml?parameter={0}&action={1}", parameterValue.ToString(), "Edit"), UriKind.Relative));
}
When we click on the "Add Anniversary Reminder" button in the AddOccasions Page the same page and the code will be executed. Like this:
Now we pass the Occasions (BirthDay and Anniversary) in the Querystring that we will fetch in the Occasion TextBox.
Step 8
Now we will write the code to show the reminders in the ListBox in the AddOccasion Page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
parameterValue = NavigationContext.QueryString["parameter"];
Action = NavigationContext.QueryString["action"];
if (Action == "Edit")
{
//btnAddBirthDayReminder.Visibility = Visibility.Collapsed;
//btnAddAnniversaryReminder.Visibility = Visibility.Collapsed;
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var a = from b in ContactDB.GetTable<ContactInfo>() where b.Id == parameterValue.ToString() select b;
foreach (var x in a)
{
if (x.BOccasion!= null)
{
lstBirthday.Visibility = Visibility.Visible;
txblkbday.Visibility = Visibility.Visible;
lstBirthday.ItemsSource = a;
}
else
{
lstBirthday.Visibility = Visibility.Collapsed;
txblkbday.Visibility = Visibility.Collapsed;
}
if (x.AOccasion != null)
{
lstAnniversary.Visibility = Visibility.Visible;
txblkAni.Visibility = Visibility.Visible;
lstAnniversary.ItemsSource = a;
}
else
{
lstAnniversary.Visibility = Visibility.Collapsed;
txblkAni.Visibility = Visibility.Collapsed;
}
}
}
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var a = from b in ContactDB.GetTable<ContactInfo>() where b.Id == parameterValue.ToString() select b;
foreach (var x in a)
{
txtName.Text = x.Name;
if (x.BirthyDay == null)
{
BirthDaydatePicker.Value = Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
else
{
BirthDaydatePicker.Value = Convert.ToDateTime(x.BirthyDay);
}
if (x.Anniversary == null)
{
AnniversarydatePicker.Value = Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
else
{
AnniversarydatePicker.Value = Convert.ToDateTime(x.Anniversary);
}
}
}
}
if (Action == "Edit")
{
using (ContactdataContext ContactDB = new ContactdataContext(strConnectionString))
{
var a = from b in ContactDB.GetTable<ContactInfo>() where b.Id== parameterValue.ToString() select b;
foreach (var x in a)
{
txtName.Text = x.Name;
if (x.BirthyDay == null)
{
BirthDaydatePicker.Value = Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
else
{
BirthDaydatePicker.Value = Convert.ToDateTime(x.BirthyDay);
}
if (x.Anniversary == null)
{
AnniversarydatePicker.Value= Convert.ToDateTime(DateTime.Now.ToShortDateString());
}
else
{
AnniversarydatePicker.Value = Convert.ToDateTime(x.Anniversary);
}
}
}
}
}
The output will be: