Bài giảng Lập trình cho thiết bị di động Windows Phone - Trần Duy Thanh - Working with the tiles notifications contacts and calendar


You want to configure which tile shows up on the home screen when the user pins your application.


Configure the Tile Template, Tile Title, and Tile Images properties in the application’s manifest file.

How It Works

The default, or primary, tile for your application is defined in the WMAppManifest.xml file located under the Properties folder of your application.


pptx75 trang | Chuyên mục: Windows Phone | Chia sẻ: dkS00TYs | Ngày: 08/06/2015 | Lượt xem: 2182 | Lượt tải: 8download
Tóm tắt nội dung Bài giảng Lập trình cho thiết bị di động Windows Phone - Trần Duy Thanh - Working with the tiles notifications contacts and calendar, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
tom sound. A Reminder has a Title, Content, and a NavigationUri. Alarms and Reminders appear as popups along with a sound. The Alarm can be snoozed or dismissed by the user pressing the buttons in the popup. A Reminder has an additional wrinkle. If the user touches outside the buttons, they are navigated to the page specified in the NavigationUri. using Microsoft.Phone.Scheduler; 45 2.2 Alarms and Reminders Creating and Scheduling the Alarm private void btnAlarm_Click (object sender, RoutedEventArgs e) { var uniqueName = Guid.NewGuid().ToString(); var alarm = new Alarm(uniqueName) { BeginTime = DateTime.Now.AddSeconds(3), Content = "You had an email!", Sound = new Uri(@"assets\sounds\windows notify.wav", UriKind.Relative) }; try { ScheduledActionService.Add(alarm); } catch (InvalidOperationException ex) { Debug.WriteLine(ex.Message); } } 46 2.2 Alarms and Reminders Creating and Scheduling the Reminder private void btnRemainder_Click(object sender, RoutedEventArgs e) { var uniqueName = Guid.NewGuid().ToString(); var reminder = new Reminder(uniqueName) { BeginTime = DateTime.Now.AddSeconds(3), Title = "Show new page", Content = "click here to show new page", NavigationUri = new Uri("/newpage.xaml", UriKind.Relative) }; try { ScheduledActionService.Add(reminder); } catch (InvalidOperationException ex) { Debug.WriteLine(ex.Message);} } 47 2.3 Toasts Toasts show users relevant and time-critical information when they’re not actually running your application. Toasts appear in the system tray showing the application icon, a title in bold font, and content in regular font. When the user clicks the toast, they’re navigated to the application. 48 2.3 Toasts private void btnToast1_Click 	(object sender, RoutedEventArgs e) { var toast = new ShellToast() { Title = "Bunny Adoption", Content = "Meet Harvey" }; toast.Show(); } 49 2.3 Toasts private void btnToast2_Click(object sender, RoutedEventArgs e) { const string path 	= "/DailyBunny.xaml?NavigatedFrom=Toast Notification"; var toast = new ShellToast() { Title = "Bunny Adoption", Content = "Meet Harvey", NavigationUri = new Uri(path, UriKind.Relative) }; toast.Show(); } 50 2.3 Toasts DailyBunny.xaml protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); string navigatedFrom; if (NavigationContext.QueryString 	.TryGetValue("NavigatedFrom", out navigatedFrom)) { //process navigatedFrom } } 51 2.4 Push Notifications Push notifications are announcements that come from outside your application and show up as toasts, tile changes, or events in your application. Notifications have three basic components: 	 The client Windows Phone 8 application that receives notifications. The Microsoft Push Notification Service. A custom service that you build using any platform that can send HTTP requests, such as ASP.NET, Web API, WCF services, and WPF. 52 2.4 Push Notifications 53 2.4 Push Notifications 54 2.4 Push Notifications 1. The client application opens a channel to the Microsoft Push Notification Service and receives a unique URI. This URI is used for all communications going forward. 2. The URI is passed from the Client Application to the custom service. 3. The custom service sends an HTTP Request to the unique URI. The body of the message is specific to the type of message you’re sending (i.e., toast, tile, or raw). 4. Microsoft Push Notification Service pushes the notification to the device running the client application. 55 3. Contacts 3.1 Querying device contacts 3.2 Save a Contact 56 3.1 Querying device contacts Single-contact choosers using Microsoft.Phone.Tasks; 57 3.1 Querying device contacts PhoneNumberChooserTask private PhoneNumberChooserTask phoneNumberChooser; phoneNumberChooser = new PhoneNumberChooserTask(); phoneNumberChooser.Completed += 	phoneNumberChooser_Completed; phoneNumberChooser.Show(); 1. Declare PhoneNumberChooser : 2. New Instance & assign Completed delegate: 3. Call Show() method : 58 3.1 Querying device contacts PhoneNumberChooserTask void phoneNumberChooser_Completed 	(object sender, PhoneNumberResult e) { if (e.TaskResult != TaskResult.OK) return; phoneNumberDisplayNameTextBlock.Text = e.DisplayName; phoneNumberTextBlock.Text = e.PhoneNumber; makePhoneCallButton.IsEnabled = true; } 4. Process Completed function to get information from PhoneNumberResult : 59 3.1 Querying device contacts EmailAddressChooserTask private EmailAddressChooserTask emailAddressChooser; emailAddressChooser = new EmailAddressChooserTask(); emailAddressChooser.Completed += 	emailAddressChooser_Completed; emailAddressChooser.Show(); 1. Declare EmailAddressChooser : 2. New Instance & assign Completed delegate: 3. Call Show() method : 60 3.1 Querying device contacts EmailAddressChooserTask void emailAddressChooser_Completed 	(object sender, EmailResult e) { if (e.TaskResult != TaskResult.OK) return; emailAddressDisplayNameTextBlock.Text = e.DisplayName; emailAddressTextBlock.Text = e.Email; sendEmailButton.IsEnabled = true; } 4. Process Completed function to get information from EmailResult : 61 3.1 Querying device contacts AddressChooserTask private AddressChooserTask addressChooser; addressChooser = new AddressChooserTask(); addressChooser.Completed += addressChooser_Completed; addressChooser.Show(); 1. Declare AddressChooser : 2. New Instance & assign Completed delegate: 3. Call Show() method : 62 3.1 Querying device contacts AddressChooserTask 4. Process Completed function to get information from AddressResult : void addressChooser_Completed 	(object sender, AddressResult e) { if (e.TaskResult != TaskResult.OK) return; addressDisplayNameTextBlock.Text = e.DisplayName; addressTextBlock.Text = e.Address; mapItButton.IsEnabled = true; } 63 3.1 Querying device contacts Make Call (ID_CAP_PHONEDIALER capability): MapsTask mt = new MapsTask(); mt.SearchTerm = addressTextBlock.Text; mt.Show(); Show Map: EmailComposeTask emailCompose = new EmailComposeTask(); emailCompose.To = emailAddressTextBlock.Text; emailCompose.Subject = "Subject here "; emailCompose.Body = "Hello from Windows Phone!"; emailCompose.Show(); Send an Email: PhoneCallTask pct = new PhoneCallTask(); pct.PhoneNumber = “098….”; pct.Show(); 64 3.1 Querying device contacts Querying contacts programmatically The single-contact Choosers are convenient for cases in which you simply need to retrieve an address, phone number, or email address for a single contact, But in many cases, you will select multiple contacts at once or perform query operations on the contacts database. The Microsoft.Phone.UserData namespace provides APIs to perform such query operations on the user’s existing contacts. ID_CAP_CONTACTS capability 65 3.1 Querying device contacts Querying contacts programmatically All contacts queries start the same way, with a new instance of the Contacts object and a call to its SearchAsync method: Contacts contactsDb = new Contacts(); contactsDb.SearchCompleted += 	contactsDb_SearchCompleted; contactsDb.SearchAsync (string.Empty, FilterKind.None, null); contactsDb.SearchAsync 	(searchTermTextBox.Text, FilterKind.DisplayName, null); 66 3.1 Querying device contacts Querying contacts programmatically void contactsDb_SearchCompleted(object sender, ContactsSearchEventArgs e) { if (e.State == null) { contactsListBox.ItemsSource = e.Results; return; } string citySearch = (string)e.State; var contactsInQueriedCity = from contact in e.Results from address in contact.Addresses where address.PhysicalAddress.City.ToUpper() == citySearch.ToUpper() select contact; contactsListBox.ItemsSource = contactsInQueriedCity; } 67 3.2 Save a Contact Problem You want the users of your application to be able to create a new contact in contacts of their Microsoft account. Solution Use the SaveContactTask in the Microsoft.Phone.Tasks namespace to configure and launch the new contact page of the built-in People application. How It Works The SaveContactTask falls under the Launcher category of tasks, ID_CAP_CONTACTS Capabilities 68 3.2 Save a Contact private void btnAddContact_Click(object sender, EventArgs e) { SaveContactTask saveContact = new SaveContactTask(); saveContact.FirstName = firstNameTextBox.Text; saveContact.LastName = lastNameTextBox.Text; saveContact.Completed += SaveContactCompleted; saveContact.Show(); } void SaveContactCompleted(object sender, SaveContactResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("Contact Added"); } else if (e.TaskResult == TaskResult.Cancel) { MessageBox.Show("Contact not added. You cancelled it."); } } 69 4. Calendar The Microsoft.Phone.UserData namespace that provides APIs for querying the user’s address books also provides APIs for querying his calendar appointments. SaveAppointmentTask 70 4. Calendar Project 71 4. Calendar MainPage.xaml 72 4. Calendar private Popup appointmentPickerPopup= new Popup(); private void findAppointmentTimeButton_Click 	(object sender, RoutedEventArgs e) { AppointmentMakerControl amc = new AppointmentMakerControl(); int meetingLength; switch (((ListPickerItem)appointmentLengthListPicker.SelectedItem).Name) { case "thirty": meetingLength = 30; break; case "sixty": meetingLength = 60; break; case "ninety": meetingLength = 90; break; case "onetwenty": meetingLength = 120; break; default: meetingLength = 30; break; } 73 4. Calendar amc.FindTimeForAppointment( appointmentDatePicker.Value.Value.Date, ((ListPickerItem)timeOfDayPicker.SelectedItem).Name, meetingLength); appointmentPickerPopup.Child = amc; appointmentPickerPopup.IsOpen = true; } 74 4. Calendar AppointmentMakerControl.xaml 75 4. Calendar private void createAppointmentButton_Click 	(object sender, RoutedEventArgs e) { SaveAppointmentTask newAppointment = new SaveAppointmentTask(); newAppointment.StartTime = AvailableTime; newAppointment.Subject = appointmentSubjectTextBox.Text; newAppointment.Location = appointmentLocationTextBox.Text; newAppointment.EndTime = AvailableTime.AddMinutes(lengthOfAppointmentInMinutes); newAppointment.Show(); } using Microsoft.Phone.Tasks; END 76 

File đính kèm: