Windows Phone 8 - 10 Using Phone Resources
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Windows Phone 8 - 10 Using Phone Resources

  • 7,764 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
7,764
On Slideshare
7,763
From Embeds
1
Number of Embeds
1

Actions

Shares
Downloads
24
Comments
0
Likes
0

Embeds 1

http://www.slideee.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Refer to the Application Policies:http://msdn.microsoft.com/en-us/library/hh184841(v=VS.92).aspxThe documentation refers to policy 2.8
  • Make the point that this is a theme that has underpinned everything we have done so far, except that it is more important for user data such as contacts and appointments.By default an application has all the capabilities requested, and if the phone is developer unlocked any program can run.However, advise people to pay attention to this file. If a capability is used that is not requested the application will fail when submitted.If a capability is not used the user may get asked to approve an access that is not required.
  • Make the point that the limitations are not technical, but to do with operational and privacy issues
  • These are the things you can get from the various sources. Note that ‘Microsoft Account’ is the new name for ‘Windows Live ID’. This used to offer email through Hotmail but has now migrated to outlook.com.
  • After you instantiate a Contacts object, you can determine what sources of contacts (accounts) have been configured on the phone by getting the Accounts property, which returns a collection of Account objects. The Kind property on that returns a StorageKind enumeration through which you can determine the kind of the account.
  • This is the method handler that would sit behind a “Load Contacts” button. It is an abridged version of the code in the example to come.Make the point that this operation is not filtered, and will return all the contacts. However, you can put filters on the search.
  • I’m using Data Binding because it is easiest here.The ContactsSearchEventArgs returns the results of the search in its Results property.In this simple example, we simply set this collection of Contacts as the DataContext of a LongListSelector, which will show the DisplayName property of each result on the screen.If they want to use a foreach construction to loop through the Results enumeration, that’s fine too.
  • This is the XAML that displays the list. We are not really talking about data binding, but it is worth making the point that it is very easy to use.
  • Follow the instructions in the Word document.
  • Make the point that a simple search is preferable to a LINQ query on the search results, and that there should be sufficient flexibility in the pre-defined search types to use this most of the time.If you feel you have time you could show the filtering on the emulator (only Roan will be displayed) but there is a lot of content to get through
  • This works in exactly the same way as for contacts. There is an example application you can use if you wish, but there are no appointments on the emulator, so it doesn’t really show much.
  • This is another reinforcement of the “user is king” paradigm. The phone will not allow an application to do substantive things (like create a contact) without the user being involved.However, the custom contact store (new to WP8) allows a particular application to store contact details for its own use.
  • Launchers and ChoosersImportant points about the application lifecycle:The application loses its foreground status when a Launcher or a Chooser is fired up, so is deactivated and becomes dormant.In the case of a Chooser, the application will be resumed automatically when the user has made their choice and exits or quits the chooser.In the case of a Launcher the application will be resumed if the user navigates back after they have used the launched item.In either case the application must be designed to handle dormant/tombstone issues if you are to use Launchers and Choosers.When your app shows a launcher or chooser, they are switching to one of the built-in applications. In the middle of that, they could decide to press the Windows button and go to run something else. Your app may never be resumed, so you must consider dormant/tombstoned issues when using launchers and choosers.   
  • Creating a ChooserIn this sample, we have a first name and last name that are going to be used as the basis of our new contact.These will get populated into the chooser that displays.Make the point that we can fill in all the contact fields for the new contact, but the user can change these in the dialog if they wish.IMPORTANT: When using Choosers, you must declare the task object (SaveContactTask) with page scope as shown here. You must instantiate the object and assign the Completed event handler in the page constructor. Then call the Show() method when you are ready to launch the Chooser task.These steps are necessary to correctly handle Tombstoning of your app, should that occur because the user has gone off and run many other apps after the Chooser is launched.   
  • Chooser CompletionMake the point that saving a contact is not really worthy of the name “chooser”, since the only choice the user makes is whether or not to save the contact; but they don’t really choose anything. Nonetheless, the SaveContactTask behaves in the same way as the other Chooser tasks and does return some data to the application.It is useful to know if the user has saved the contact or not, and that is what is returned.Note however that if the user changes the name of the contact (or any other details) and then saves the changed contact data, your application has no way of knowing this has been done. So it is not a guarantee that the item has been saved with exactly the properties that your application set before launching the task. The only way to be sure is to retrieve the contact details after it has been saved and check it against the information you provided.   
  • A new feature in Windows Phone8 is the ability of an app to create a custom contacts store just for its own use. An app can create contacts programmatically in its own store without the user needing to give confirmation.Interestingly, these contacts appear in the People Hub in just the same way as any other contacts, so can be searched, and can be linked with contact cards from other accounts. Think of an apps custom contact store as just another ‘account’ on the phone.This feature is targeted at companion apps to websites which have their own user community or contacts capability. The mobile app can download details of the user’s contacts fro the backend systems and insert them into a custom contacts store on the phone, whereupon they appear integrated into the users’ People Hub.Custom contact cards can have visible and invisible custom fields on them.
  • Here is an example of opening the Contact Store for this app and inserting a StoredContact into it. This contact also stores a custom field called “Password” in the extended properties, which is the IDictionary<string, object> where you store custom fields.
  • In the People Hub, the user can look at the contact card for a custom contact in exactly the same way as with contacts for other accounts. A user cannot edit the fields created by the application, but these contact cards do offer an ‘edit’ facility. Editing in this case is done in exactly the same way as with facebook contacts – a linked card is created in the phone contacts list (known as the ‘Microsoft Acccount’) which is linked to the contact card created by the application. The user can edit fields on the linked contact, but the Contact details they view in the People Hub is the amalgamated view combining all the linked contact cards.This is a bit confusing. The best way to show it is to demonstrate it in the next demo.
  • Follow the instructions in the Word document.
  • Other Save TasksThe other ‘Save’ Choosers are the SaveEmailAddressTask, SavePhoneNumberTask and SaveRingtoneTask. They all work in a similar fashion to the SaveContactTask.  
  • You can use Launchers and Choosers in your Windows Phone applications to enable users to perform common tasks. By using Launchers and Choosers, you help provide a consistent user experience throughout the Windows Phone platform. At the same time, by using Launchers and Choosers, you can distinguish your applications by providing additional functionality.The Launcher and Chooser APIs invoke distinct built-in applications that replace the currently running application.A Chooser is an API that launches one of the built-in applications, such as the Contacts application or the camera, through which the user completes a task. When the new application appears, the user can choose to complete or cancel the task. When the user closes the new application, the calling application is usually reactivated and supplied with data and status. Examples of Chooser tasks include selecting a contact’s email address, selecting a photo from the phone, and saving a new ringtone.Choosers can return data and status to your application.A Launcher is an API that launches one of the built-in applications, such as the Contacts application or the web browser, through which the user completes a task. They work in the same way as Choosers, but the difference is that Launchers do not return data or status to your application. Examples of Launcher tasks include composing an email, sharing a link on a social network, and opening the browser to a specific website.
  • Chooser TasksThe slide shows a complete list of all the Choosers available in Windows Phone 8.There are complete examples of how to program each of these tasks in the Windows Phone developer documentation on MSDN: http://msdn.microsoft.com/en-us/library/ff769556(v=VS.92).aspx    
  • Launcher TasksThese are all the Launchers. Launchers are similar to Choosers in that you are invoking one of the phone built-in experiences from your application. However, Launchers differ from Choosers in that they do not have a Completed event and do not return any data to your application.As with Choosers, your app is deactivated and made dormant when the Launcher is started.Your app will be reactivated when – or if – the user backs out of the launched experience and navigates back to your app.There are complete examples of how to program each of these tasks on MSDN  
  • The SaveAppointmentTask is a new launcher that allows you to create an appointment. Unlike the SaveContactTask, this is not a chooser and does not return status to the calling application.
  • As with other Launchers and Choosers, this launches abuilt-in application, in this case the New Appointment application.
  • Also new in Windows Phone 8, is the ability to launch certain of the built-in application using the Windows.System.Launcher.LaunchUriAsync() method. This API is compatible with a similar API available in Windows 8.In many cases, this provides an alternative to the Launcher APIs, but in a few, such as the Lock Screen settings dialog shown on the slide, this is the only way to launch that application.
  • This is the full list of URIs for built-in application in Windows Phone 8.
  • There are also some zune: URIs that launch different experiences of the built-in Windows Phone Store app.
  • Alarms and RemindersA scheduled notification is a dialog box that pops up on the screen at a specified time, similar to the notifications displayed by the phone’s built-in applications. The dialog box displays some customizable text information to the user and allows the user to dismiss the notification or postpone it until later. If the user taps the notification, the application that created it is launched. Notifications can be configured to launch once or multiple times on a recurring schedule. Note that the schedules for these notifications are accurate only within a range of one minute. In other words, the notification can be launched up to one minute after it was scheduled. There are two types of scheduled notifications, Alarm and Reminder. An Alarm allows you to specify a sound file to play when the notification is launched. When you create a Reminder, you can specify a relative navigation URI pointing to a page in your application, including query string parameters. When the Reminder is displayed to the user and the user taps on it, your application is launched and is passed the URI that you specified. Note that there is a limit of 50 Alarms or Reminders at a time per application. A notification will not automatically cause that application to start when the notification fires, only if the user taps on it.    
  • Alarms and Reminders look quite similar, and behave quite similarly.For an Alarm:The title is always the local language equivalent of AlarmYou can supply your own alarm sound fileIf tapped, launches the app at the regular launch pageFor Reminders:You can specify the titleYou cannot override the sound that plays – that is whatever the user has configured on the phone for remindersYou can associate a deep link URI with a reminder, so if tapped, can launch your application at any page. You can use this to create context-sensitive experiences for your users so that when they tap on a Reminder, it launches at a page that relates to the event the reminder was set for.
  • The “Egg Timer” ApplicationThis is a very simple timer application. Make the point that this could be used for things like car park timing.  In actual fact, this would not be a very good egg timer application, since the schedules for these notifications are accurate only within a range of +/- 60 seconds! In other words, the notification can be launched up to one minute after it was scheduled leading to harder boiled eggs than you might have wanted.
  • Creating a ReminderMake the point that for this reminder there is no recurrence. We are only boiling one egg. The recurrence can be set to daily, monthly, end of month and yearly.You can also set an expiration time when a recurring reminder is to stop.    
  • Reminder HousekeepingMake the point that, as with other tasks, even though a reminder has fired it will still exist in the system.If we want to, we can use the ScheduledActionService.Find() method to find a reminder and update it or remove it, alternatively we can delete the reminder and make a new one.  
  • For demonstration notes please look in the “Demonstration” folder and open the corresponding Word document.
  • There are many ways an application can use the camera hardware, from the very simple to the very sophisticated.New in Windows Phone 8 is the ability to register an app that can perform real-time video/image processing as a ‘Lens’, which means that it appears on the ‘Lenses’ menu in the built-in Camera application, so can be chosen by the user to apply real-time effects or processing to the camera image. This might be useful for applying effects or overlaying augmented reality onto the captured video stream.Also new in Windows Phone 8 is the Windows.Phone.Media.Capture APIs including PhotoCaptureDevice and AudioVideoCaptureDevice. These APIs offers a variety of camera and photo properties that you can use to fine-tune your app’s photography. We do not cover programming these classes further in this course.
  • The CameraCaptureTask is a standard Chooser with a completed event as we have seen before.
  • Note that photos captured in this way are not added automatically to the camera roll (the users’ own pictures)
  • The PhotoCamera is the class that abstracts the camera behaviours.It lives in the Microsoft.Devices namespace.
  • Explain that the viewfinderBrushin here is assigned to the camera by the call of SetSourcein the method that sets up the camera.
  • When we press a button on the screen we want to trigger a photograph.When the photograph has been taken the camera_CaptureImageAvailable event will fire.
  • Note that the MediaLibrary class lives in the XNA framework, this is how we save the file.The filename is made of the number of ticks in the date plus the jpg extension.Make the point that this action must take place on the right thread, which is what the Dispatcher.BeginInvoke is for.
  • This saves the image into a BitmapImage which is displayed on the screen in an <Image> element (PictureImage is the name of an <Image> element in XAML).
  • Note that this continues from the previous slide.The variable b here is the BitmapImage object we loaded with the captured image in the previous slide.We can take that and load it into a WriteableBitmap, and using that we can save it as a JPEG into the local folder.The variable filename holds the name of the file being created in isolated storage.
  • Use the demo sequence in the Word document
  • Now we are going to look at how we can use the PhotoCamera class to access video data from the camera in real-time, and to apply some real time processing to it.You can register such an application as a ‘Lens’ which allows your app to have visibility right from within the built-in Camera application.An icon on the app bar allows the user to choose from the available lenses, showing a UI like this shown on the slide.We will walk though the creation of a Funky Camera lens.
  • To register as a Lens, you must add this XML to the WMAppmanifest.xml file.
  • If you are registering your application as a lens, you must supply three icons.
  • When the user selects your application from the ‘Select a Lens’ UI, your app will be launched not to the URI of a specific page but to a custom URI that identifies that the app has been launched from the Lens chooser.You must add a special class called a UriMapper to intercept these launches and route the request to a specific page. We will look at the implementation of a UriMapper in the File and Protocol Associations module. Fortunately, our app only has one page, and in this case you do not need a UriMapper as the app will launch automatically to that page.
  • We use the PhotoCamera class to get access to the live video feed.
  • This is the guts of the funky image processing. Remind everyone that we could read these values and do what we like with them.This method is going to get called for each pixel in the screen
  • This is part of the setup.You’d call it when the camera page is loaded
  • This bit starts the message pump running.Make the point that it is running on a new thread, alongside the application.
  • Note that there is some thread synchronisation going on here to.This is to make sure that the thread and the camera don’t get stuck.Make the point that this code is all in the sample that they can have in a minute
  • Note that because the bitmap is on the Silverlight page, and thus controlled by the Silverlight thread, we have to use a dispatcher to perform this copy.
  • Follow the instructions in the Word Document.
  • Windows Phones come with the ability to auto-upload all pictures you take to SkyDrive.If you run your own cloud repository service for photos, you can create an app for Windows Phone that has an associated Resource Intensive Background Agent. If you put the XML shown into the app manifest, your app will be identified as a background Photo Uploader and will appear alongside SkyDrive as a possible destination for auto-uploading.Then when the phone is on charge and resource intensive background agents can run, your background agent can examine the Camera Roll and identify new photos and upload them to your cloud repository. You will have to implement all the logic to make this happen.Your foreground app could allow the user to set preferences, or to record credentials for accessing your cloud repository.
  • Yep, we’ve got one, and you can record sound from it.The Microphone walkthrough in the docs is very good, but we’re not going to cover it further in this course.
  • Windows Phone supports multiple sensors that allow apps to determine the orientation and motion of the device. These sensors enable the development of apps where the physical device itself is a means of user input. Typical uses for this functionality include motion-controlled games and augmented reality apps.There is an extensive library in the .NET API set which has been present since Windows Phone OS 7.0 and this is still available to developers today. When programming with that, the combined motion API, which combines and processes input from all sensors, is the simplest way to obtain motion and orientation information. For apps that require it, Windows Phone also provides APIs to retrieve data from the individual sensors.We will not cover this API further in this module.For new development, it is recommended that you use the Windows Phone Runtime Sensors API which is compatible with sensor programming using WinRT on Windows 8.
  • The Windows Phone Runtime Sensors library allows programmatic access to hardware sensors.Accelerometer: Detects acceleration along three axes (x, y, and z). Inclinometer: Detects angle of incline along three axes (pitch, roll, and yaw). Gyrometer: Detects angular velocity along three axes. Compass: Detects heading in degrees relative to magnetic north (and due north when integrated with onboard GPS). Orientation: Combines the data from the accelerometer, compass, and gyrometer sensors to provide smoother and more sensitive rotation data than can be obtained from any of the sensors alone. This combined sensor data is also known as “Sensor Fusion.” Note that there is no API on Windows Phone to access the Light Sensor.
  • Make the point that you must always check to see if the sensor is there before you use it.There is a software and a hardware thing here. Different phones support different hardware sensors.Make the point that you might find it useful to access the Accelerometer as a fallback.
  • The report interval is specified in milliseconds. An application must set the report interval to a non-zero value prior to registering an event handler or calling GetCurrentReading. This informs the sensor implementation that it should allocate resources to satisfy the application’s requirements.Conversely, when an application is finished with the sensor, it should explicitly return the sensor to its default report interval by setting it to zero. This is important for power conservation, especially when using a language that may keep the sensor object active for an indefinite period prior to garbage collection.The application should consult the MinimumReportInterval property prior to setting the report interval to ensure that the sensor can honor the requested report interval. Setting a value below the minimum supported interval will either trigger an exception or have undefined results.Although the application can set this value to request a particular report interval, the driver will determine the actual report interval, based on internal logic. For example, the driver might use the shortest report interval that is requested by any caller.
  • An application may register this event handler to obtain sensor readings. Applications can set the frequency of this event by setting the ReportInterval property.In fact, the application must establish a desired ReportInterval because this informs the sensor driver that resources should be allocated to satisfy the requirements of the application. In XAML-based development event driven content is fine, but in games there is an expectation that sensors will be polled, which is covered in the next slide
  • This is true for all the sensors, compass, accelerometer etc.An application may use this method to poll the sensor for the current reading as an alternative to registering a ReadingChanged event handler. This would be the preferred alternative for an application that updates its user interface at a specific frame rate. Whether polling once or many times, the application must establish a desired ReportInterval. This informs the sensor driver that resources should be allocated to satisfy subsequent polling requests Before using the return value from this method, the application must first check that the value is not null. (If the value is null and you attempt to retrieve it, Windows will generate an exception.)
  • Make the point that video playback is very easy on the Windows phone platform.
  • If the source is a url it will be loaded and displayed
  • If the source is a url
  • Lots of stuff here.Tell folks that all the demonstrations are available and would make the basis of a number of application ideas.

Transcript

  • 1. Oliver Scheer Senior Technical Evangelist Microsoft Deutschland http://the-oliver.com Using Phone Resources
  • 2. Topics • Using the Contacts and Calendars in Windows Phone • Launchers and Choosers • The Windows Phone Camera • Taking still images • Manipulating the video stream • The Windows Phone Microphone • The Windows Phone Sensors • The Motion sensor • Video Content
  • 3. Reading Contacts and Calendars 3
  • 4. Your Obligations • Before an application uses contact data in an application you must inform the user and request their permission • You must also ensure that your application manages contact data in a secure way • It would be completely unacceptable for your application to make a copy of the contact list and upload this or use it for spamming 4
  • 5. Application Capabilities • Before an application can use the Contacts and Appointments data on a phone the user has to authorise this capability • Applications identify their capabilities in the WMAppManifest.xml file which is part of the application project • Visual Studio 2012 provides a GUI which can be used to manage these capabilities • Not all the capabilities are enabled when you make a new application 5
  • 6. Contacts and Calendar Providers • A Windows Phone application can read contact and calendar information from the different accounts • Windows Live • Exchange (Outlook, Google) • Facebook • Aggregated accounts (Twitter, LinkedIn etc) • The precise abilities depend on the source of the information being used 6
  • 7. Data Sources Data provider Contact name Contact picture Other contact data Calendar appointments Microsoft Account Yes Yes Yes Yes Exchange Accounts (Contacts are from the local address book only, not the global address list.) Yes Yes Yes Yes Mobile Operator Address Book Yes Yes Yes No Facebook Yes Yes No No Windows Live Aggregated Networks (Twitter, LinkedIn, and so on) No No No No 7
  • 8. Finding Data Sources • Each account has a property that allows an application to identify the source of that account • The StorageKind enumeration has values for all the account types available on Windows Phone Contacts cons = new Contacts(); // Get the accounts from which contacts are available on the phone IEnumerable<Account> accounts = cons.Accounts; if (accounts[0].Kind == StorageKind.Outlook) { // Outlook account }
  • 9. Reading Contacts • This starts off a contacts load request • This is not a filtered search, all contacts are returned using Microsoft.Phone.UserData; ... private void loadButton_Click(object sender, RoutedEventArgs e) { Contacts cons = new Contacts(); cons.SearchCompleted += new EventHandler <ContactsSearchEventArgs>(cons_SearchCompleted); cons.SearchAsync(String.Empty,FilterKind.None, null); }
  • 10. Displaying Contacts • This runs when the search returns • It uses data binding to display the results on the screen in a ListBox void cons_SearchCompleted(object sender, ContactsSearchEventArgs e) { try{ //Bind the results to the user interface. ContactResultsData.DataContext = e.Results; } catch (System.Exception){ } }
  • 11. Data Binding • This is the XAML that databinds the DisplayName property of the contact to a series of items in a list <ListBox Name="ContactResultsData" ItemsSource="{Binding}" Height="347" Margin="24,0,0,0" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
  • 12. Demo Demo 1: Contact Display
  • 13. Filtering Searches • By setting the FilterKind value on the search an application can apply a filter to the results • There are a number of filters available • Display Name, Pinned to start, Email address, Phone number • For advanced filtering a LINQ query could be used on the result enumeration • Although this might be a slow operation cons.SearchAsync("Ro", FilterKind.DisplayName, null); // Find all the contacts with names beginning "Ro"
  • 14. Displaying Appointments • There is an analogous set of methods for finding appointments • An application can search in a particular account type, for appointments in a particular time range • There is also a search method to search a particular account • The reply is provided as a call back • There are no appointments built into the emulator 14 apps.SearchAsync( new DateTime(2000, 1, 1), new DateTime(2013, 1, 1), null);
  • 15. Creating a Contact 15
  • 16. Creating a Contact • An application cannot create a contact without the user being aware this action is being performed • This ensures that the user is always aware of what is going on when they are using the program • Applications can create their own custom contact store to store contact details for use in that application • This appears on the People Hub and in contact searches on that phone • To create a contact we use a “Chooser” which will display a confirmation dialog and give the user the option to create the contact or not • The chooser is called asynchronously and will generate an event that tells the application whether the action succeeded or not 16
  • 17. Launchers and Choosers • A Chooser is used when an application wants to perform a task and get a response from the user • Select a contact or image • On completion of the choice the application is resumed • A Launcher called when application wants to perform a task • Visit a web page or place a phone call • The user can return to the application via the navigation stack • In either case the application is made dormant/tombstoned while the task completes
  • 18. Creating a Chooser • This creates a chooser to display the save contact dialog using Microsoft.Phone.Tasks; ... SaveContactTask saveContact; //Declare with page scope public MainPage() { saveContact = new SaveContactTask(); saveContact.Completed += new EventHandler <SaveContactResult>(saveContact_Completed); } private void MakeContactButton_Click(object sender, RoutedEventArgs e) { saveContact.FirstName = FirstNameTextBox.Text; saveContact.LastName = LastNameTextBox.Text; saveContact.Show(); }
  • 19. Chooser Completion • This method is called when the chooser returns • Some choosers return the result of selection the user has made • This chooser returns whether the user saved the contact or not void saveContact_Completed(object sender, SaveContactResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show("Saved OK"); } }
  • 20. Custom Contacts Store • It is only possible for an application to add contacts to any of the users‟ configured accounts by using the SaveContactTask • An application can create a Custom Contacts store to which it can add contacts directly without user confirmation • Contacts added in this way will show up in the People Hub on the phone and also in searches for contacts • These contacts can also be linked with other contacts on the phone • Your application can also add custom properties (for example passwords or additional user data) to entries in the custom contacts store
  • 21. Creating a Custom Contact • This method creates a custom contact with a custom password property • This will not cause a Chooser to be displayed, the contact is stored directly async private void MakeContactButton_Click(object sender, RoutedEventArgs e) { ContactStore store = await ContactStore.CreateOrOpenAsync(); StoredContact contact = new StoredContact(store); contact.RemoteId = Guid.NewGuid().ToString(); contact.GivenName = FirstNameTextBox.Text; contact.FamilyName = LastNameTextBox.Text; IDictionary<string, object> props = await contact.GetExtendedPropertiesAsync(); props.Add("Password", PasswordEntry.Text); await contact.SaveAsync(); }
  • 22. Using Custom Contacts • An application can search through the custom contacts that it has created • Custom contacts created by an application can be linked with other contacts on the phone • They can also be created from vCard electronic business cards • If the user tries to edit a custom contact the phone will offer to create a copy of the account and link it to the custom one, as shown on the right 3/19/201422
  • 23. Demo Demo 2: Make a Contact
  • 24. Other Save Tasks • There are a number of other „save‟ Choosers available • SaveEmailAddressTask • Save an email address • The address can be added to a contact • SavePhoneNumberTask • Save a phone number • SaveRingtoneTask • Save a sound file as a ringtone
  • 25. Launchers vs Choosers • A Chooser allows the given action to return a result • Often the result is an item that has been chosen • Sometimes the result is a status report from an action that may succeed or fail • The application can bind to an event which will be fired once the choice has been made or the action performed • The action may never be completed if the user goes off and does something else • A Launcher causes a change of context into the target of the launch • The current application is made dormant or tombstoned • If the user returns to the application it will be Activated or Launched at that point 3/19/201425
  • 26. Chooser Tasks Action Chooser Task Launch the Wallet application and add an item*NEW* AddWalletItemTask Search contacts to return addresses, phone numbers and email addresses AddressChooserTask, PhoneNumberChooserTask, EmailAddressChooserTask Select a picture from the media store PhotoChooserTask Capture a picture using the camera CameraCaptureTask Invite players to a multi-game session GameInviteTask Save a new contact SaveContactTask Save email addresses or phone numbers SaveEmailAddressTask, SavePhoneNumberTask Save ringtones SaveRingtoneTask
  • 27. Launcher Tasks Action Task Show Bing maps and directions BingMapsTask, BingMapsDirectionsTask Launch the network connections settings dialog ConnectionSettingsTask Send an email EmailComposeTask Show maps and directions*NEW* MapsTask, MapsDirectionsTask Download and update offline maps*NEW* MapDownloaderTask, MapUpdaterTask Search the Marketplace and find applications MarketplaceSearchTask, MarketPlaceHubTask Show Marketplace App Details and Review Apps MarketplaceDetailTask, MarketplaceReviewTask Play media MediaPlayerLauncher Place a phone call PhoneCallTask Create an Appointment*NEW* SaveAppointmentTask Share a link, status or photo/video*NEW* on a social network ShareLinkTask, ShareStatusTask, ShareMediaTask Send an SMS message SMSComposeTask Start a search using Bing SearchTask Open a web page WebBrowserTask
  • 28. Creating an Appointment with a Launcher (New in WP8) • This code creates a 1 hour appointment 2 hours into the future • It will have a reminder set for 15 minutes before the event SaveAppointmentTask saveAppointmentTask = new SaveAppointmentTask(); saveAppointmentTask.StartTime = DateTime.Now.AddHours(2); saveAppointmentTask.EndTime = DateTime.Now.AddHours(3); saveAppointmentTask.Subject = "Meet with Rob"; saveAppointmentTask.Location = "Somewhere nice in Hull"; saveAppointmentTask.Details = "Discuss important stuff."; saveAppointmentTask.IsAllDayEvent = false; saveAppointmentTask.Reminder = Reminder.FifteenMinutes; saveAppointmentTask.AppointmentStatus = Microsoft.Phone.UserData.AppointmentStatus.Busy; saveAppointmentTask.Show();
  • 29. Creating an Appointment • When the appointment is created the launcher runs in the same way as for an address book entry • The user can assign the appointment to a particular calendar and save it • Note that this is a launcher event • There is no data returned from the task • If your application wants to see if the appointment was created it will have to go and look for it • When the launcher completes the application is Activated from Dormant or Tombstoned 3/19/201429
  • 30. • As an alternative to the Launcher APIs, you can use the LaunchUriAsync method to launch system applications • Some built-in applications are *only* available by this technique Launching a Built-in App Using LaunchUriAsync (New in WP8) 3/19/201430 private void LockSettingsButton_Click(object sender, RoutedEventArgs e) { // Launch the lock screen settings dialog Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings-lock:")); }
  • 31. URI scheme Description http:[URL] Launches the web browser and navigates to the specified URL. mailto:[email address] Launches the email app and creates a new message with the specified email address on the To line. Note that the email is not sent until the user taps send. ms-settings-accounts: Launches the Account Settings app. ms-settings-airplanemode: Launches the Airplane Mode Settings app. ms-settings-bluetooth: Launches the Bluetooth Settings app. ms-settings-cellular: Launches the Cellular Settings app. ms-settings-emailandaccounts: Launches the email and accounts settings app. ms-settings-location: Launches the Location Settings app. ms-settings-lock: Launches the Lock Screen settings app. ms-settings-wifi: Launches the Wi-Fi Settings app. Launching Built-in Apps Use LaunchUriAsync to launch many of the built-in apps 3/19/2014Microsoft confidential31
  • 32. Launching Built-in Apps (cont) 3/19/2014Microsoft confidential32 URI scheme Description zune:navigate?appid=[app ID] Launches the Windows Phone Store and shows the details page for the specified app. zune:reviewapp Launches the Store and shows the review page for the calling app. zune:reviewapp?appid=[app ID] Launches the Store and shows the review page for the specified app. zune:search?[search parameter]=[value] Launches the Store and searches for the specified content. zune:search?keyword=[search keyword] &contenttype=app Launches the Store and searches for apps by keyword. zune:search?publisher=[publisher name] Launches the Store and searches for items by publisher name.
  • 33. 3/19/2014Microsoft confidential33 Alarms and Reminders on Windows Phone 8
  • 34. Alarms and Reminders • Time-based, on-phone notifications • Persist across reboots • Adheres to user settings • Consistent with phone UX • These are displayed whether the application is running or not • Reminder notifications can be deep linked to an application page • Notifications can fire once, or repeatedly at configurable intervals • Limit of 50 Alarms and Reminders at a time per application 34
  • 35. Alarms vs Reminders? Alarms • Title always “Alarm” • Sound customization • Snooze and Dismiss • If tapped, launches app at main page • No stacking Reminders • Can set Title • Uses user‟s selected Alarm sound • Snooze and Dismiss • If tapped, launch app at page you specify • Integrates with other Reminders
  • 36. The “Egg Timer” Application • This is a simple two page Silverlight application • The user sets the time using the slider and then presses the Start Timer button to create a notification • When the notification fires the “Egg Ready” page is displayed if the user clicks through to the application 36
  • 37. Creating a Reminder • This code creates a reminder and adds it as a scheduled service • The value eggTime holds the length of the delay • This code also sets the url of the page in the application 37 using Microsoft.Phone.Scheduler; ... eggReminder = new Reminder("Egg Timer"); eggReminder.BeginTime = DateTime.Now + new TimeSpan(0, eggTime, 0); eggReminder.Content = "Egg Ready"; eggReminder.RecurrenceType = RecurrenceInterval.None; eggReminder.NavigationUri = new Uri("/EggReadyPage.xaml", UriKind.Relative); ScheduledActionService.Add(eggReminder);
  • 38. Reminder Housekeeping • Reminders are identified by name • This code finds the “Egg Timer” reminder and then removes it from the scheduler 38 Reminder eggReminder = ScheduledActionService.Find("Egg Timer") as Reminder; if ( eggReminder != null ) { ScheduledActionService.Remove("Egg Timer"); }
  • 39. Using the Camera 40
  • 40. The Windows Phone Camera • There are a number of ways the camera can be used by an application • The application can launch the CameraCaptureTask chooser to allow the user to take a photograph • The application can use the PhotoCamera class to capture photos or stream video data from the camera • Can use this for product recognition or augmented reality • Use the PhotoCaptureDevice class for advanced photo capture and AudioVideoCaptureDevice for advanced video capture • A real-time video processing application can be registered as a “Lens” • It can be selected by the user from a menu of available lens types • The Lens application will provides a viewfinder display and performs video processing on the signal from the camera
  • 41. cameraTask = new CameraCaptureTask(); cameraTask.Completed += new EventHandler<PhotoResult> (cameraTask_Completed); cameraTask.Show(); Capturing a photo • This task launches the camera so that the user can take a picture • The cameraTask_Completed event fires when the picture is taken • The emulator will return an image containing a small block
  • 42. void cameraCapture_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { photoImage.Source = new BitmapImage( new Uri(e.OriginalFileName)); } } Capture complete • When capture has been made, completed method is executed • This version just displays the image on the screen • You can access the photo stream data to save it
  • 43. In application capture • It is also possible to capture an image from within your application • An application can also display a viewfinder • The application can also access the video data directly and use this for augmented reality or to create a customised viewfinder • eg. Barcode scanners • The PhotoCamera class provides camera control and access to the camera video screen 44
  • 44. The PhotoCamera class • This creates a camera and binds a handler to the captured event • The viewfinderBrush source is set to the camera using Microsoft.Devices; ... PhotoCamera camera; ... camera = new PhotoCamera(); //Set the VideoBrush source to the camera viewfinderBrush.SetSource(camera); camera.CaptureImageAvailable += new EventHandler<ContentReadyEventArgs> (camera_CaptureImageAvailable);
  • 45. Displaying the Viewfinder • This is the rectangle in the xaml for the camera viewfinder page • This will display the viewfinder on the screen • The source for the viewfinder brush is set to the camera <Rectangle Width="320" Height="240" HorizontalAlignment="Left" > <Rectangle.Fill> <VideoBrush x:Name="viewfinderBrush" /> </Rectangle.Fill> </Rectangle>
  • 46. Initiating the Capture • This is the event handler for the photo button • It asks the camera to take a picture • The current camera settings are used for this • You can override these (for example turn the flash on or off) by setting properties on the camera instance private void PhotoButton_Click(object sender, RoutedEventArgs e) { camera.CaptureImage(); }
  • 47. Saving the image • This saves the image in the camera roll using Microsoft.Xna.Framework.Media; ... void camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e) { Deployment.Current.Dispatcher.BeginInvoke(delegate() { string fileName = DateTime.Now.Ticks.ToString() + ".jpg"; MediaLibrary library = new MediaLibrary(); library.SavePictureToCameraRoll(fileName e.ImageStream); }); }
  • 48. Displaying the image • This saves the image into a BitmapImage which is displayed on the screen in an <Image> using System.Windows.Media.Imaging; ... void camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e) { Deployment.Current.Dispatcher.BeginInvoke(delegate() { BitmapImage b = new BitmapImage(); b.CreateOptions = BitmapCreateOptions.None; b.SetSource(e.ImageStream); PictureImage.Source = b; }); }
  • 49. Saving to the Local Folder • This saves the image in the local folder using System.IO; using System.IO.IsolatedStorage; ... using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) { WriteableBitmap bitmap = new WriteableBitmap(b); bitmap.SaveJpeg(targetStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100); } }
  • 50. Demo Demo 3: In application camera
  • 51. Creating a Lens • A Lens is a custom camera application which can be accessed from within the camera application • An application is flagged as a Lens application by setting a flag in the manifest and providing icons that can be used to browse for the Lens when the camera is in use • I‟ve created a FunkyCamera lens application which I have registered in this way 52
  • 52. Creating a Lens application • This text must be added to the WMAppManifest.xml file for the application, just after the <Tokens> section • There is no GUI for this alteration, you have to edit the XML directly <Extensions> <Extension ExtensionName="Camera_Capture_App" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5631}" TaskID="_default" /> </Extensions>
  • 53. Adding the Lens Icons • Three Icons are required, one for each Windows Phone screen size • WVGA 173 × 173 AssetsLens.Screen-WVGA.png • 720p 259 × 259 AssetsLens.Screen-720p.png • WXGA 277 × 277 AssetsLens.Screen-WXGA.png • They are all placed in the Assets folder of the application • Use a transparent background to match the Windows Phone color scheme 3/19/201454
  • 54. Lens Startup • You can create a URI Mapper to direct the application to the page that implements the viewfinder for the Lens • This can use the string “Viewfinderlaunch” in the destination uri that is activated when the user selects the lens application • Alternatively, if the program only contains a single page this page is displayed • If the user “backs out” of the lens application they will be returned to the camera 3/19/201455
  • 55. Image Processing in a Lens • The PhotoCamera class is used to provide access to the video stream • This can be used by applications to perform image processing on the live data • Augmented reality • Scanning applications • We are going to use it to make the funky camera display a funky image 56
  • 56. Funky Image Processing • This separates out the primaries and adds an offset to each • It is called for each pixel in the image internal int FunkyColor(int color) { int a = color >> 24; int r = (color & 0x00ff0000) >> 16; int g = (color & 0x0000ff00) >> 8; int b = (color & 0x000000ff); r += redOffset; g += greenOffset; b += blueOffset; return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF); }
  • 57. Starting the Camera • This creates the camera and the bitmap that will contain the image processed output • It binds to the event fired when the camera is ready camera = new Microsoft.Devices.PhotoCamera(); // Create the destination for the processed image wb = new WriteableBitmap(640, 480); this.ProcessedImage.Source = wb; // Start the image pump when the camera is ready camera.Initialized += new EventHandler<CameraOperationCompletedEventArgs> (camera_Initialized);
  • 58. Starting the Camera • When the camera is ready we start the thread that will pump frames into our image processor • This will run alongside our application void camera_Initialized(object sender, CameraOperationCompletedEventArgs e) { pumpARGBFrames = true; ARGBFramesThread = new System.Threading.Thread(PumpARGBFrames); ARGBFramesThread.Start(); }
  • 59. Getting the Image Data • This code grabs the preview buffer from the camera and processes it int[] ARGBPx = new int[640 * 480]; ... captureEvent.WaitOne(); pauseFramesEvent.WaitOne(); //Thread sync with camera //Copies the current viewfinder frame into a buffer camera.GetPreviewBufferArgb32(ARGBPx); //Conversion to funky colours for (int i = 0; i < ARGBPx.Length; i++) { ARGBPx[i] = FunkyColor(ARGBPx[i]); }
  • 60. Drawing the Image Data • This code writes the processed pixels back to a writeable bitmap that is displayed on the screen private WriteableBitmap wb; ... pauseFramesEvent.Reset(); Deployment.Current.Dispatcher.BeginInvoke(delegate() { //Copy to WriteableBitmap ARGBPx.CopyTo(wb.Pixels, 0); wb.Invalidate(); pauseFramesEvent.Set(); });
  • 61. Demo Demo 4: Funky camera
  • 62. • You can also create an application with an associated “resource intensive” background task that can auto-upload pictures that the user may take to a cloud storage service • This is a resource intensive background process and may run when the phone is on charge • The user can select your app from the Camera Settings • The application must set the extension shown above and display a settings page where the user can set authentication and upload options Creating an Auto-Uploader for Photos <Extensions> <Extension ExtensionName="Photos_Auto_Upload" ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5632}" TaskID="_default" /> </Extensions>
  • 63. Using the Microphone
  • 64. The Windows Phone Microphone • The Windows Phone microphone can capture 16 bit audio • Microphone input is managed as part of the XNA framework • It is possible to record sound, process the audio and either store or replay it • There is a complete example of how to do this on MSDN
  • 65. Windows Phone 8 Sensors 3/19/2014Microsoft confidential66
  • 66. Sensors Available • There are a number of different sensors: • Accelerometer • Compass • Gyroscope • Inclinometer • Orientation • All the sensors are used in the same way: • They will fire an event when they have a reading • Two APIs for managed code developers: • Microsoft.Devices.Sensors (Windows Phone OS 7.1 API set) • Windows.Devices.Sensors (Windows Phone Runtime)
  • 67. The Windows Runtime Sensors Library • The Windows Phone Runtime sensors APIs are compatible with sensor usage in WinRT on Windows 8 • Accelerometer – returns G-force values with respect to the x, y, and z axes • All phones have an accelerometer • Inclinometer - returns pitch, roll, and yaw values that correspond to rotation angles around the x, y, and z axes, respectively • The inclinometer readings are derived from multiple sensors • Gyrometer - returns angular velocity values with respect to the x, y, and z axes • Compass - returns a heading with respect to True North and, possibly, Magnetic North • OrientationSensor - returns a rotation matrix and a Quaternion that can be used to adjust the user's perspective in a game application • Combines the data from the accelerometer, compass, and gyrometer – known as “Sensor Fusion” using Windows.Devices.Sensors;
  • 68. Determining Sensor Availability • All the sensor classes have a GetDefault() method • This method only returns values for hardware that has been integrated into the computer by the manufacturer • Returns null if the sensor is not available on that device • All phones will have an accelerometer // Determine whether we have a gyro on the phone _gyrometer = Gyrometer.GetDefault(); if (_gyrometer != null) { // Establish the report interval (units are milliseconds) _gyrometer.ReportInterval = 100; _gyrometer.ReadingChanged += _gyrometer_ReadingChanged; } else { MessageBox.Show("No gyrometer found"); }
  • 69. Starting and Stopping a Sensor • Application must set the report interval to a non-zero value prior to registering an event handler or calling GetCurrentReading to activate it • When finished with the sensor, set it to zero • Check the MinimumReportInterval property • Setting a value below the minimum supported interval will either trigger an exception or have undefined results. • Sensor driver will determine the actual report interval // Establish the report interval (units are milliseconds) uint reportInterval = 100; if (_gyrometer.MinimumReportInterval > reportInterval) { reportInterval = _gyrometer.MinimumReportInterval; } _gyrometer.ReportInterval = reportInterval;
  • 70. Using the Sensor ReadingChanged event • Register the ReadingChanged event handler to obtain sensor readings • Must set the ReportInterval property first _gyrometer.ReportInterval = 100; _gyrometer.ReadingChanged += _gyrometer_ReadingChanged; … private void _gyrometer_ReadingChanged(Gyrometer sender, GyrometerReadingChangedEventArgs args) { Dispatcher.BeginInvoke(() => { GyrometerReading reading = args.Reading; X_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX); Y_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY); Z_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ); }); }
  • 71. Getting Readings By Polling a Sensor • An application can poll the sensor for the current reading as an alternative to registering a ReadingChanged event handler • The preferred alternative for an application that updates its user interface at a specific frame rate • Must still establish a desired ReportInterval before polling in order to activate the sensor // Alternative to ReadingChanged event, call GetCurrentReading() to poll the sensor GyrometerReading reading = _gyrometer.GetCurrentReading(); if (reading != null) { X_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX); Y_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY); Z_Reading.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ); }
  • 72. Video Content
  • 73. Video on the Phone • An application can contain a single MediaElement that can play video • The sample above plays a resource file that is part of the project containing the application • You can find a list of supported codecs here: http://msdn.microsoft.com/en-us/library/ff462087.aspx <MediaElement Name= "MediaPlayback" Source= "myvideo.wmv" AutoPlay="True"/>
  • 74. Streaming Video on the Phone • If you want to stream from the internet, just replace the source file with a url • This implementation has the location hard coded into the XAML • You can also do this under program control <MediaElement Name= "MediaPlayback" Source="http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv" AutoPlay="True"/>
  • 75. Controlling Playback • The MediaElement exposes methods that can be used to control the media playback • An application can also determine the properties of the media stream • To determine if it can be paused for example private void pauseButton_Click(object sender, RoutedEventArgs e) { MediaPlayback.Pause(); }
  • 76. Smooth Streaming • Windows Phone also supports Smooth Streaming • This is an adaptive streaming that manages the quality of the video signal in response to the abilities of the network connection • Playback quality is managed in real time to handle changes in network performance during viewing • It uses a server side plugin and client code on the viewing device • You can download the viewing software here: http://smf.codeplex.com
  • 77. Review • Applications can use phone resources by means of Launchers and Choosers • Launchers start a behaviour, choosers can return a result • Launchers and Choosers interrupt the running of the application • There are two types of scheduled notifications, Alarm and Reminder • An Alarm allows you to specify a sound file to play when the notification is launched • When you create a Reminder, you can specify a deep link URI • Applications can capture images and video feeds from the camera • Applications can create “Lens” behaviours that are accessed from the in-phone camera • Applications can provide upload behaviors to upload photographs • Applications can use sensors and determine which of them are present • The MediaContent element provides for video playback 78
  • 78. The information herein is for informational purposes only an represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.